根据复选框列表中选择的值编写SQL语句

时间:2016-12-02 10:53:06

标签: c# sql asp.net

我有两个复选框列表,一个用于分区,另一个用于课程。一个部门可以有很多课程,所以我想要的是让我的用户选择他们想要的任何部门,因为只有10个然后在另一个复选框列表中显示由已经选择的部门提供的课程。

前体信息:

private DataTable GetData(string query)
{
    string constr = ConfigurationManager.ConnectionStrings["DatabaseName"].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))
    {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = query;

                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;

                    using (DataSet ds = new DataSet())
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;

                    }
                }
            }
    }
}

这是我将数据绑定到Divisions复选框列表的代码:

protected void GetDiv()
{
    string query = " Select distinct uio.OFFERING_ORGANISATION as [Div], ou.FES_FULL_NAME as [Division] From UNIT_INSTANCE_OCCURRENCES uio " +
                   " inner join ORGANISATION_UNITS ou on uio.OFFERING_ORGANISATION = ou.ORGANISATION_CODE " +
                   " inner join REPORTYEARS ry on uio.CALOCC_OCCURRENCE_CODE = ry.CAL_OCC " +
                   " Where ry.REPORT_YEAR = (select DETAILS from EF_REFERENCE_DATA Where REC_TYPE = 'Rep_Year') and uio.OFFERING_ORGANISATION is not null Order By [Division] ";

    cbDivSelect.DataSource = GetData(query);
    cbDivSelect.DataTextField = "DIVISION";
    cbDivSelect.DataValueField = "DIV";
    cbDivSelect.DataBind();
}

我用它来获取所选分区的列表:

protected void cbDivSelect_SelectedIndexChanged(object sender, EventArgs e)
{
        foreach (ListItem item in cbDivSelect.Items)
        {
            if (item.Selected) Divisions.Add(item);
        }
    }

然后显示课程:

protected void GetCourse()
    {
        foreach(ListItem item in Divisions)
        {
            string GetCourses = "SELECT distinct Course_Code,Course_Code + ' - ' + Marketing_Title AS COURSE, Parent FROM e_prospectus WHERE (Div = '" + item.Value.ToString() + "') ORDER BY Course_Code";
            cbCourseSelect.DataSource = GetData(GetCourses);
            cbCourseSelect.DataTextField = "COURSE";
            cbCourseSelect.DataValueField = "Course_Code";
            cbCourseSelect.DataBind();
        }
    }

现在它现在只显示已选择的列表中的最低路线,我假设因为我不断更改数据源并且它没有累积但是我有什么方法可以更改我的代码容纳我想要的东西?感谢

2 个答案:

答案 0 :(得分:0)

        string divisions = string.empty ; 
     foreach(ListItem item in Divisions)
            {
           divisions  = divisions  + item.Value.tostring() + ",";
            }

    if (divisions != string.empty)
  { 
    divisions = divisions.Remove(divisions.Length -1, 1)
     string GetCourses = "SELECT distinct Course_Code,Course_Code + ' - ' + Marketing_Title AS COURSE, Parent FROM e_prospectus 
    WHERE (Div in (" + divisions + ")) ORDER BY Course_Code";
                cbCourseSelect.DataSource = GetData(GetCourses);
                cbCourseSelect.DataTextField = "COURSE";
                cbCourseSelect.DataValueField = "Course_Code";
                cbCourseSelect.DataBind();
    }

答案 1 :(得分:0)

DataTable创建为DataSource,将Merge()的结果GetData()添加到其中。

这将在内存中构建整个集合。根据您希望使用FakeisMe方法构建一个查询以便同时从数据库中获取所有课程的课程数量可能要快得多。