2016-12-02 26 views
0

我有兩個複選框列表,一個用於分區,一個用於課程。一個部門可以有許多課程,所以我想要做的是讓我的用戶選擇他們想要的任何部門,因爲只有10個,然後在另一個複選框列表中顯示課程,這些部門由已選擇的部門提供。根據在複選框列表中選擇的值編寫SQL語句

前兆信息:

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(); 
     } 
    } 

現在它只顯示列表中已選擇的最低路線,我假設因爲我一直在更改數據源並且它不會積累,但有什麼辦法可以將我的代碼更改爲容納我想要的東西?感謝

+0

請使用參數化查詢/存儲過程。不要使用字符串連接來建立查詢 - 讓自己容易受到意外的語法錯誤以及更重要的SQL注入攻擊的影響。 – ADyson

+0

'GetData(GetCourses)'返回一個列表假設?在這種情況下,我只需創建一個用作'DataSource'和'AddRange()'GetData()'的結果的列表。最好使用'BindingList',因爲它應該關注更新綁定。 – Mats391

+0

我完全知道參數化查詢,我只是喜歡把我的查詢建立爲字符串,然後將它們作爲參數添加它們 –

回答

0

創建的GetData()一個DataTableDataSourceMerge()結果進去。

這將在內存中構建整個集合。取決於您希望使用FakeisMe方法構建一個查詢以將所有課程同時從數據庫中取出的課程數量可能會快得多。

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(); 
    } 
+0

不會這樣做會留下一個逗號,它會爲最後一個分區提供不正確的語法嗎? –

+0

你說得對。謝謝,編輯答案 – FakeisMe

+0

,在我自己添加這個,它的工作:) –