使用不同的连接字符串进行级联下拉列表

时间:2016-05-16 07:01:18

标签: javascript c# asp.net

我的项目中有两个dropdownlist。我的整个项目使用一个connectionstring但是对于这两个下拉列表,我想使用不同的connectionstring

我在web.config文件中定义了所有连接字符串。以下是代码: -

<td style="width: 15%" class="field">
    <select id="cmbRecdDept" runat="server" style="width: 25%" onchange="FunEmpFillDept()">
        <option value="0">--Select--</option>
    </select>
</td>
<td style="width: 15%" class="field">
    <select id="cmbRecdEmp" runat="server" style="width: 25%" onchange="FunSelEmpRecd()">
        <option value="0">--Select--</option>
    </select>
</td>

后面的代码是

ObjPriDT = ObjPriDal.ExecuteDataTable("select distinct master_mkey, Type_Desc from type_mst_a a join emp_mst b on a. master_mkey=b.department_mkey where  b.status in ('A','S','R') order by Type_Desc");
    cmbRecdDept.DataSource = ObjPriDT;
    cmbRecdDept.DataTextField = "Type_Desc";
    cmbRecdDept.DataValueField = "master_mkey";
    cmbRecdDept.DataBind();
    cmbRecdDept.Items.Insert(0, new ListItem("---Select---", "0"));
第一个列表的

onchange我绑定第二个列表,其代码如下: -

function FunEmpFillDept() {
        document.getElementById('cmbRecdEmp').innerHTML = "";
        var ObjPriOption = document.createElement("Option");
        ObjPriOption.text = "ALL";
        ObjPriOption.value = "0";
        //document.getElementById('txtEmpID').value="0";
        document.getElementById('cmbRecdEmp').add(ObjPriOption);
        StrPriFnName = "FunFillEmp(" + document.getElementById('cmbRecdDept').value + ")";
        var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        ObjPriXMLHTTP.open("GET", "FrmInwardXMLHTTP.aspx?para=" + StrPriFnName, false);
        ObjPriXMLHTTP.send("");
        if (ObjPriXMLHTTP.responseText != "") {
            StrPriRow = ObjPriXMLHTTP.responseText.split('|');
            for (IntPriI = 0; IntPriI < StrPriRow.length - 1; IntPriI++) {
                StrPriCol = StrPriRow[IntPriI].split('~');
                var ObjPriOption = document.createElement("Option");
                ObjPriOption.text = StrPriCol[1];
                ObjPriOption.value = StrPriCol[0];
                document.getElementById('cmbRecdEmp').add(ObjPriOption);
            }
        }
    }

及其功能FunFillEmp位于

之下
public static string FunFillEmp(object[] args)
{
    string StrPriReturn = "";
    DataAccessLayer ObjPriDt = new DataAccessLayer();

    DataTable dt = new DataTable();
    dt = ObjPriDt.ExecuteDataTable("select mkey,Emp_Name,emp_card_no from emp_mst where department_mkey=" + args[0].ToString() + " and status in ('A','S','R') order by 2");
    if (dt.Rows.Count > 0)
    {
        for (int IntpriI = 0; IntpriI < dt.Rows.Count; IntpriI++)
        {
            StrPriReturn += dt.Rows[IntpriI][0].ToString() + "~" + dt.Rows[IntpriI][1].ToString() + "~" + dt.Rows[IntpriI][2].ToString() + "|";
        }
    }
    return StrPriReturn;
}

如何对这两个下拉列表使用不同的connectionstring

更新

我的dataAccesslayer代码

public DataAccessLayer(string connectionstring, Providers provider)
{
    strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    switch (provider)
    {
        case Providers.SqlServer:
            objFactory = SqlClientFactory.Instance;
            break;
        case Providers.OleDb:
            objFactory = OleDbFactory.Instance;
            break;
        case Providers.Oracle:
            objFactory = OracleClientFactory.Instance;
            break;
        case Providers.ODBC:
            objFactory = OdbcFactory.Instance;
            break;
        case Providers.ConfigDefined:
            string providername = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
            switch (providername)
            {
                case "System.Data.SqlClient":
                    objFactory = SqlClientFactory.Instance;
                    break;
                case "System.Data.OleDb":
                    objFactory = OleDbFactory.Instance;
                    break;
                case "System.Data.OracleClient":
                    objFactory = OracleClientFactory.Instance;
                    break;
                case "System.Data.Odbc":
                    objFactory = OdbcFactory.Instance;
                    break;
            }
            break;
    }
    objConnection = objFactory.CreateConnection();
    objCommand = objFactory.CreateCommand();
    objConnection.ConnectionString = strConnectionString;
    objCommand.Connection = objConnection;
}

1 个答案:

答案 0 :(得分:1)

看来你必须修改你的DataAccessLayer类,在构造函数中传递连接字符串/添加一个方法来选择连接字符串

- 更新 - 修改DataAccessLayer

public DataAccessLayer(string connectionstring, Providers provider)
{

    strConnectionString = ConfigurationManager.ConnectionStrings[connectionstring].ConnectionString;

然后将其实例化为(对于sql server)

DataAccessLayer ObjPriDt = new DataAccessLayer(ConfigurationManager.ConnectionStrings["ConnectionString"],Providers.SqlServer);

为其他连接指定

例如,如果它的ConnectionString2

DataAccessLayer ObjPriDt = new DataAccessLayer(ConfigurationManager.ConnectionStrings["ConnectionString2"],Providers.SqlServer);