Wednesday, December 14, 2011

Best way to change the ConnectionString dynamically in C#



Update the connection string dynamically.

using System.Data.SqlClient;
using System.Data.EntityClient;

public virtual string UpdateConnectionString(string database)
{
var _connectionString = "Data Source=.;Initial Catalog=SampleDB;Integrated Security=SSPI;MultipleActiveResultSets=True"
var sqlBuilder = new SqlConnectionStringBuilder(_connectionString);
sqlBuilder.InitialCatalog = database.ToString();
return sqlBuilder.ConnectionString;
}


Change Entity Framework ConnectionString dynamically

public virtual string UpdateEFConnectionString(string newConnectionString)
{
var csBuilder = new EntityConnectionStringBuilder();
csBuilder.ProviderConnectionString = newConnectionString;
return csBuilder.ConnectionString;
}

1 comment:

  1. Thanks for sharing excellent information. Your
    website is very cool.
    Premium
    Premium

    ReplyDelete