在C#中使用ConnectionStrings實現(xiàn)多數(shù)據(jù)庫切換功能可以通過以下步驟實現(xiàn):
<connectionStrings>
<add name="DB1" connectionString="Data Source=Server1;Initial Catalog=Database1;User Id=sa;Password=password;" providerName="System.Data.SqlClient" />
<add name="DB2" connectionString="Data Source=Server2;Initial Catalog=Database2;User Id=sa;Password=password;" providerName="System.Data.SqlClient" />
</connectionStrings>
string connectionString = ConfigurationManager.ConnectionStrings["DB1"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
// 使用connection對象執(zhí)行數(shù)據(jù)庫操作
}
string connectionString = ConfigurationManager.ConnectionStrings["DB2"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
// 使用connection對象執(zhí)行數(shù)據(jù)庫操作
}
通過以上步驟,就可以在C#中實現(xiàn)多數(shù)據(jù)庫切換功能。在需要切換數(shù)據(jù)庫時,只需修改連接字符串的名稱即可連接到另一個數(shù)據(jù)庫。