How to create connection string dynamically in C#

11,663

You can create dynamic connection string with SqlConnectionStringBuilder Class in C# as follows.

for more details pls check http://msdn.microsoft.com/en-us/library/dce36088.aspx

private void Form1_Load(object sender, EventArgs e)
{
     SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder();
     connectionString.DataSource = @".\SQLEXPRESS";
     connectionString.InitialCatalog = "MyDatabase";
     connectionString.IntegratedSecurity = true;
     MessageBox.Show(connectionString.ConnectionString);
}
Share:
11,663
rgy
Author by

rgy

Updated on June 05, 2022

Comments

  • rgy
    rgy almost 2 years

    How to create connection string dynamically in C#, instead of creating it using string concatenation?