簡體   English   中英

使用C#在一個單行數據庫中插入多個清單列表框值

[英]inserting multiple checkedlistbox values in one single row database using c#

我正在制作一個Windows窗體,其中所有清單列表中的值都存儲在一行中。 但是,它僅存儲一個值。

問題是,是否可以將所有檢查表值存儲在僅存儲其分配的ID的一行中?

這是我下面的先前代碼。 如果您發現任何錯誤,或者對如何進行編輯有任何想法,這對我來說將是很大的幫助。 謝謝!

foreach (DataRowView item in this.checkedListBox1.CheckedItems)
{
    NpgsqlCommand cmd = new NpgsqlCommand("Insert into name(eid, name, famid) Values (@eid, @name, @famid)", conn);
    cmd.Parameters.AddWithValue("@eid", textBox1.Text);
    cmd.Parameters.AddWithValue("@name", textBox2.Text);
    string value = item.Row[0].ToString().Split(',');
    cmd.Parameters.AddWithValue("@famcon", value);
    cmd.ExecuteNonQuery();
}

由於您使用的是C#,因此請使用此代碼,但必須將默認名稱更改為您的名稱。

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string Str = "";
            if (checkedListBox1.CheckedItems.Count > 0)
            {
                for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
                {
                    if (Str == "")
                    {
                        Str = checkedListBox1.CheckedItems[i].ToString();
                        label1.Text = Str;
                    }
                    else
                    {
                        Str += ", " + checkedListBox1.CheckedItems[i].ToString();
                        label1.Text = Str;
                    }
                }
                // Make your connection to the DataBase and insertion code starting within that area.
                MessageBox.Show("Your selection is for :" + Str);
            }
            else
            {
                MessageBox.Show("Please select one name at least to work it out!");
            }
            while (checkedListBox1.CheckedItems.Count > 0)
            {
                checkedListBox1.SetItemChecked(checkedListBox1.CheckedIndices[0],false);
            }
                        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + ex.ToString());
        }
    }

暫無
暫無

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

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