我正在尝试连接某些数据库。我正在使用一个类使用以下代码连接到我的连接字符串:
private static BaseDados instance;
public static BaseDados Instance
{
get
{
if (instance == null)
instance = new BaseDados();
return instance;
}
}
private string strLigacao;
private MySqlConnection ligacaoBD;
public BaseDados()
{
//ligação à`enter code here` bd
strLigacao = ConfigurationManager.ConnectionStrings["empresaA"].ToString();
ligacaoBD = new MySqlConnection(strLigacao);
ligacaoBD.Open();
}
然后我用以下内容更改连接字符串:
Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["empresaB"].ConnectionString = @"Server=localhost;Database=" + DropDownList1.SelectedValue.ToString() + " ;Uid=root;Pwd=Qwerty!123;";
configuration.Save();
Response.Redirect("**********");
嗯,这种方法只能同时支持一个数据库,因为有人对我说这是一个单例。 有人知道如何更改它以支持多个数据库吗?