简体   繁体   English

C#更新数据库中listBox中的每个项目

[英]C# Update Database for every Item in listBox

I have created a Database that has an ID column (aitisi_ID) and a category column(kat_aitisis). 我创建了一个数据库,该数据库具有一个ID列(aitisi_ID)和一个类别列(kat_aitisis)。 I want to Update the kat_aitisis column for every ID that shows up on my listBox with the string kat1 . 我想用字符串kat1更新listBox上显示的每个ID的kat_aitisis列。

Edit 2: the column 'aitisi_ID' is int 编辑2:列'aitisi_ID'是int

I have this so far,but it doesnt work (cn is my SqlConnection and cmd is SqlCommand): 到目前为止,我有这个,但是它不起作用(cn是我的SqlConnection,cmd是SqlCommand):

// Edit: This is the code that I use to add Items to my listBox
private void loadlist()
    {
        listBox1.Items.Clear();
        listBox2.Items.Clear();
        cn.Open();
        string kat = "Εγκυρη";
        cmd.CommandText = ("SELECT aitisi_ID,moria FROM Table2 WHERE kat_aitisis='"+kat+"' ORDER BY moria DESC");
        dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                listBox1.Items.Add(dr[0].ToString());
                listBox2.Items.Add(dr[1].ToString());
            }
        }
        cn.Close();
    }


string kat1 = "aaaaa";

        cn.Open();
        for (int i = 0; i < listBox1.Items.Count; i++)
        {

            listBox1.SetSelected(i, true);
            cmd.CommandText = "UPDATE Table2 SET kat_aitisis='"+kat1+"' WHERE aitisi_ID='"+listBox1.SelectedIndex+"'";
            cmd.ExecuteNonQuery();
            cmd.Clone();

        }  
        cn.Close();

If the column 'aitisi_ID' is of numeric type the correct update query is 如果列“ aitisi_ID”为数字类型,则正确的更新查询为

    string kat1 = "aaaaa";
    cmd.CommandText = "UPDATE Table2 SET kat_aitisis=@kat WHERE aitisi_ID=@id";
    cmd.Parameters.AddWithValue("@kat", kat1);
    cmd.Parameters.AddWithValue("@id", 0);
    cn.Open();
    for (int i = 0; i < listBox1.Items.Count; i++)
    {
        cmd.Parameters["@id"].Value = Convert.ToInt32(listBox1.Items[i]));
        cmd.ExecuteNonQuery();
    }  
    cn.Close();

Created a parameterized query with two params and set with initial values. 创建具有两个参数的参数化查询并设置初始值。
One parameter of type string with an invarian value (kat1) The second parameter is of integer type with a dummy initial value. 字符串类型的一个参数具有不变值(kat1),第二个参数是具有虚假初始值的整数类型。
Then, in the loop only the varying parameter (@id) will be updated. 然后,在循环中,仅变化的参数(@id)将被更新。
The value for the second parameter is taken from the Item collection of ListBox1 that is filled with integers (all the items are passed to the update value) and converted back to int as expected by the datatable 第二个参数的值取自用整数填充的ListBox1的Item集合(所有项目均传递给更新值),并按数据表的预期转换回int

You don't need to wrap the Id in a single quote block. 您无需将ID括在单引号中。 Since it is an integer, SQL server will be happy with WHERE aitisi_ID='"+listBox1.SelectedIndex+"', but you do need to ToString() it. 由于它是整数,因此SQL Server将对WHERE aitisi_ID ='“ + listBox1.SelectedIndex +”'满意,但您需要ToString()它。

WHERE aitisi_ID="+listBox1.SelectedIndex.ToString()+"

Also try creating code less general, with a foreach : 也可以尝试使用foreach创建不太通用的代码:

    foreach(object i in listBox1.Items)

You don't need single quotes for integers, just for strings. 您不需要为整数单引号,只需为字符串。 Try to post your error, debug result, try to put breakpoints and give more info please. 尝试发布您的错误,调试结果,尝试放置断点并提供更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM