Wednesday, August 14, 2013

How to Create connection string dynamically asp.net/c#


 Below code explains how can we create connection string dynamically in web.config file.  
 make a connection string in web.config as given below:  
 <add key="conString" value="Data Source={0};Initial Catalog={1};User ID=sa;Password=pass;Trusted_Connection=False;Persist Security Info=True" />  
 C# code  
 private static string ConnectionString(string dbservername,string dbname)  
 {        
      string connectionString = string.Empty;  
      if (!string.IsNullOrEmpty(dbservername))  
      {  
           connectionString = ConfigurationManager.AppSettings["conString"].ToString();  
           connectionString = string.Format(connectionString, dbservername, dbname);  
      }  
      else  
      {  
           connectionString = "make default connection string";  
      }        
      return connectionString;  
 }  

0 comments:

Post a Comment