管理我的网站连接字符串取决于下拉选择。
在web.config
我有:
<add name="DUM01"
connectionString="Data Source=CHETAN-PC\SQLEXPRESS;Initial Catalog=DUM01;Integrated Security=True"
providerName="System.Data.SqlClient"/>
<add name="DUM02"
connectionString="Data Source=CHETAN-PC\SQLEXPRESS;Initial Catalog=DUM02;Integrated Security=True"
providerName="System.Data.SqlClient"/>
班级档案:
public class ConnectionStr
{
public int iClientId = 0;
public string sCon = string.Empty;
public string StrConnection(string strClientName)
{
if (strClientName != string.Empty)
{
if (strClientName == "")
{
sCon = Convert.ToString(ConfigurationManager.ConnectionStrings[""]);
}
else if (strClientName == "DUM02")
{
sCon = Convert.ToString(ConfigurationManager.ConnectionStrings["DUM02"]);
}
else if (strClientName == "DUM01")
{
sCon = Convert.ToString(ConfigurationManager.ConnectionStrings["DUM01"]);
}
}
return sCon;
}
}
下拉:
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="tr">
<asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
<asp:ListItem Text="Simons Holidays" Value=""></asp:ListItem>
<asp:ListItem Text="RVL Logistics" Value="DUM02"></asp:ListItem>
<asp:ListItem Text="Simon Shipping" Value="DUM01"></asp:ListItem>
</asp:DropDownList>
Dropdown SelectIndexChanged:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string sClientName = Convert.ToString(DropDownList1.SelectedItem.Value);
Session["ClientName"] = sClientName;
ConnectionStr objConnectionStr = new ConnectionStr();
string strConnection = objConnectionStr.StrConnection(sClientName);
if (Session["ClientName"] != null)
{
sClientName = Convert.ToString(Session["ClientName"]);
}
strConnection = objConnectionStr.StrConnection(sClientName);
SqlConnection scon1 = new SqlConnection(strConnection);
}
当我需要使用任何网络表单时,我使用:
public partial class Webforms_VendorsWithBoot : System.Web.UI.Page
{
public string sClientName = string.Empty;
public static string strConnection = string.Empty;
public static DataTable TableData = new DataTable();
public static string brcode = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ClientName"] != null)
{
sClientName = Convert.ToString(Session["ClientName"]);
}
ConnectionStr objConnectionStr = new ConnectionStr();
strConnection = objConnectionStr.StrConnection(sClientName);
}
}
现在情况是数据库是DUM01被选中我需要DUM02同时可用,反之亦然,因为两个数据库中的某些表是相同的,在某些webforms中我需要在两个数据库中插入数据。
如何解决这个问题?提前谢谢..
答案 0 :(得分:0)
我建议您使用数据库复制。你说你有相同的数据库与相同的表。 Check this out。 复制可帮助您管理主数据库和辅助数据库中的数据。您不必担心用于插入数据的数据库。这将自动反映出来。
答案 1 :(得分:0)