Code for Dynamically set the database for the project
a)In button click event you can set the databse using database properties.
b)In another button click you can access from configuration settings for SQLdataadapter
sample code in c#.net shown below:
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Data.ConnectionUI.DataConnectionDialog dcd=new Microsoft.Data.ConnectionUI.DataConnectionDialog();
Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSources(dcd);
if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dcd) == DialogResult.OK)
{
textBox1.Text = dcd.ConnectionString;
ConnectionStringsSection Section=new ConnectionStringsSection();
ConnectionStringSettings Setting;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Section = (ConnectionStringsSection)(config.GetSection("connectionStrings"));
if (Section.ConnectionStrings["ConString"]==null)
{
Section.ConnectionStrings.Add(new ConnectionStringSettings("ConString", dcd.ConnectionString));
}
Setting = Section.ConnectionStrings["ConString"];
Setting.ConnectionString = dcd.ConnectionString;
ConfigurationManager.RefreshSection("connectionStrings");
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
}
else
{
textBox1.Text = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select *from Testing",System.Configuration.ConfigurationManager.ConnectionStrings["ConString"].ConnectionString );
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment