簡體   English   中英

使用CheckedListBox值的SELECT語句

[英]SELECT Statement Using CheckedListBox Values

我需要使用查詢結果填充第二個CheckedListBox,查詢的值取決於初始CheckedListBox中選中的選項,每次選中或取消選中另一個框時都要重新查詢。

另外有用的是重用我正在使用的SqlConnection的方法。 這似乎很簡單,但是我不知道該怎么做。 現在我已經知道了。

    private void Connection()
    {
        SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Project;Integrated Security=True");
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter
        ("SELECT [StandardCode], c.CanStatement, [StandardDetail] FROM [dbo].[StandardCodesAndDetails] s JOIN dbo.CanStatements c ON c.StandardsID=s.ID", conn);
        adapter.Fill(ds);
        this.lstBoxStandardCodes.DataSource = ds.Tables[0];
        this.lstBoxStandardCodes.DisplayMember = "StandardCode";
        //conn.Close();
    }

在初始化時,它將填充第一個CheckedListBox(lstBoxStandardCodes),並且我正在尋找一種方法來調整最后兩行以接受變量輸入,以將其用於我的第一個問題(如果有道理)。 lstBoxStandardCodes將更改為lstBoxStandardDetails(在這種情況下),並且“ StandardCode”將變為“ StandardDetails”。 通過將private void Connection()更改為private void Connection(string Member)或類似方法,最后一點似乎很容易,但是其余的對於我來說並不是很容易。

如果需要進一步說明,請告訴我。 謝謝。

在這里,我在本地進行了測試,效果很好。

    private void Connection()
    {
        //Please use a using statement as below
        using (SqlConnection conn = new SqlConnection("Data Source=PWALTON-ACER;Initial Catalog=pwalton-test;Integrated Security=True"))
        {
            conn.Open();

            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter
            ("SELECT [StandardCode], c.CanStatement, [StandardDetail] FROM [dbo].[StandardCodesAndDetails] s JOIN dbo.CanStatements c ON c.StandardsID=s.ID", conn);
            adapter.Fill(ds);
            lstBoxStandardCodes.DataSource = ds.Tables[0];
            lstBoxStandardCodes.DataTextField = "StandardCode";
            lstBoxStandardCodes.DataBind(); //Don't forget to DataBind()

            lstBoxStandardDetails.DataSource = ds.Tables[0];
            lstBoxStandardDetails.DataTextField = "StandardDetail";
            lstBoxStandardDetails.DataBind(); //This list won't populate if you don't DataBind()
        }
    }

沒有包含有關支持表的任何信息,但是我推斷出可能是這樣的(基於原始文章中的內聯SQL): SQL Fiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM