3

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

rgy
  • 66
  • 1
  • 1
  • 6

2 Answers2

9

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);
}
ramya
  • 2,350
  • 6
  • 31
  • 57
0

you better use ADO .NET entity model. check this please: Tutoriel ADO .NET

Naourass Derouichi
  • 773
  • 3
  • 12
  • 38