Hello my application must connect to a sql server express database
somewhere on the intranet. I've created a login form and am taking
values from the app.conf file as follow in the Form Ctor
cfg =3D
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfg_cs =3D
cfg.ConnectionStrings.ConnectionStrings["Adm.Properties.Settings.MyConnecti=
onString"];
;
if (cfg_cs =3D=3D null)
cfg_cs =3D new
ConnectionStringSettings("Adm.Properties.Settings.MyConnectionString",
"Data Source=3D;Initial Catalog=3DMyDb;User ID=3D; Password=3D;",
"System.Data.SqlClient");
//enumerate servers
fabrique =3D DbProviderFactories.GetFactory("System.Data.SqlClient");
DbDataSourceEnumerator e =3D fabrique.CreateDataSourceEnumerator();
DataTable dt =3D e.GetDataSources();
this.comboBoxServer.DataSource =3D dt;
this.comboBoxServer.DisplayMember =3D "ServerName";
this.comboBoxServer.ValueMember =3D "ServerName";
conn_build =3D fabrique.CreateConnectionStringBuilder();
conn_build.ConnectionString =3D cfg_cs.ConnectionString;
//datasource
if (conn_build.ContainsKey("Data Source"))
this.comboBoxServer.SelectedItem =3D conn_build["Data
Source"].ToString();
//user
if (conn_build.ContainsKey("User ID"))
{
this.textBoxUsrName.Text =3D conn_build["User ID"].ToString();
textBoxUsrPassword.Focus();
}
Now when the user press enter
//update fields
if(conn_build.ContainsKey("Data Source"))
conn_build.Remove("Data Source");
DataRowView drv =3D comboBoxServer.SelectedItem as DataRowView;
conn_build.Add("Data Source", drv.Row["ServerName"].ToString() + "\\" +
drv.Row["InstanceName"].ToString());
if(conn_build.ContainsKey("User ID"))
conn_build.Remove("User ID");
conn_build.Add("User ID", textBoxUsrName.Text.ToString());
if (conn_build.ContainsKey("Password"))
conn_build.Remove("Password");
conn_build.Add("Password", textBoxUsrPassword.Text.ToString());
DbConnection conn =3D fabrique.CreateConnection();
conn.ConnectionString =3D conn_build.ConnectionString;
//now test the connection
try
{
conn.Open();
conn.Close();
//if we get here connection was ok
//remove password and save back config file
conn_build.Remove("Password");
cfg_cs.ConnectionString =3D conn_build.ConnectionString;
cfg.Save();
}
catch(Exception exc)
{
MessageBox.Show(exc.Message.ToString(), "La tentative de connexion
a =E9chou=E9e.", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Now what is happening is that values are correct in the
connectionBuilder but are not save back to the config file of the
application ?
What is wrong with this? And is there a way to store an encypted
version of the string ?
One last question? Is there a way to query the user rights connecting
to sql server. I have two users in database one with select only
privilege and another one (admin) with update, delete, etc.
Thanks in advance
mateo