如何使用 C# 从数据库中检索 checkedlistbox 值

How to retrieve checkedlistbox value from Database using C#

我想从 ms access 数据库获取值到 checkedListBox。 它适用于 ComboBox 和 TextBox 但我不知道如何使用 checkedListBox_prodlinecheckedListBox_owner. (我数据库字段只有一个值)

private void button_clone_Click(object sender, EventArgs e)
    {
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "SELECT * from PPAPdatabase where [PSW ID]=" + txt_c_PSW_ID.Text + "";
            OleDbDataReader dr = null;
            dr = command.ExecuteReader();                
            while (dr.Read())
            {
                comboBox_PPAP.Text = (dr["Reason"].ToString());
                checkedListBox_prodline.Text = (dr["Production Line"].ToString());                    
                checkedListBox_owner.Text = (dr["Owner"].ToString());              
                txt_comment.Text = (dr["Comment"].ToString());                                                          
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error has occurred: " + ex.Message,
                    "Important Note",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1);
        }
        finally
        {
            connection.Close();
        }            

如有任何帮助,我们将不胜感激!

看看CheckedListBox.SetItemChecked。如果您的项目是字符串。

var productLine = dr["Production Line"].ToString();
for (var i = 0; i < checkedListBox_prodline.Items.Count; i++)
{
    var item = checkedListBox_prodline.Items[i] as string;
    checkedListBox_prodline.SetItemChecked(i, productLine == item);
}