How to create connection string dynamically in C#, instead of creating it using string concatenation?
            Asked
            
        
        
            Active
            
        
            Viewed 1.2k times
        
    3
            
            
        - 
                    1possible duplicate of [When to use StringBuilder?](http://stackoverflow.com/questions/1825781/when-to-use-stringbuilder) – Yochai Timmer May 12 '13 at 19:34
- 
                    Look at the class [SqlConnectionStringBuilder](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx) – Steve May 12 '13 at 19:35
- 
                    What do you mean `dynamically` ? as in a ConnString that can be changed at runtime, or ? – Jens Kloster May 12 '13 at 19:44
- 
                    ConnString that can be created at runtime. – rgy May 12 '13 at 19:53
2 Answers
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
