|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Modifying Configuration file at application runtimesomewhere 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 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cfg_cs = cfg.ConnectionStrings.ConnectionStrings["Adm.Properties.Settings.MyConnectionString"]; ; if (cfg_cs == null) cfg_cs = new ConnectionStringSettings("Adm.Properties.Settings.MyConnectionString", "Data Source=;Initial Catalog=MyDb;User ID=; Password=;", "System.Data.SqlClient"); //enumerate servers fabrique = DbProviderFactories.GetFactory("System.Data.SqlClient"); DbDataSourceEnumerator e = fabrique.CreateDataSourceEnumerator(); DataTable dt = e.GetDataSources(); this.comboBoxServer.DataSource = dt; this.comboBoxServer.DisplayMember = "ServerName"; this.comboBoxServer.ValueMember = "ServerName"; conn_build = fabrique.CreateConnectionStringBuilder(); conn_build.ConnectionString = cfg_cs.ConnectionString; //datasource if (conn_build.ContainsKey("Data Source")) this.comboBoxServer.SelectedItem = conn_build["Data Source"].ToString(); //user if (conn_build.ContainsKey("User ID")) { this.textBoxUsrName.Text = 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 = 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 = fabrique.CreateConnection(); conn.ConnectionString = 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 = conn_build.ConnectionString; cfg.Save(); } catch(Exception exc) { MessageBox.Show(exc.Message.ToString(), "La tentative de connexion a échouée.", 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 |
|||||||||||||||||||||||