简体   繁体   English

如何使用SQL查询从另一个列表框向列表框添加值?

[英]How to add values to listbox from another listbox using SQL query?

User clicks a btnAdd and it transfers items to a listBox1 . 用户单击btnAdd ,然后将项目传输到listBox1 Now I want to create a query that creates a loop from the listBox1 to SELECT FROM a table from SQL and add the result items to listBox2 现在,我想创建一个查询,该查询创建一个从listBox1到SQL的SELECT FROM表的循环,并将结果项添加到listBox2

I have this sample code but it's not working. 我有此示例代码,但无法正常工作。 Can someone help me? 有人能帮我吗?

public void add()
{
    var con = new DBConnection();
    try
    {
        for (int i = 0; i < listBServices.Items.Count; i++)
        {
            SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = '" +
                listBServices.Items.ToString() + "';", con.Connection);
            SqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())
            {
                int price = rd.GetInt32(0);
                listBPrice.Items.Add(price.ToString());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

I get this exeption: 我得到这个证据:

在此处输入图片说明

Check your connection and use the Using code block to close the connection automatically. 检查您的连接并使用使用代码块自动关闭连接。

 string str = "Data Source=(local);Initial Catalog=Northwind;"
        + "Integrated Security=SSPI";
 string queryString =
        "SELECT price FROM price WHERE service ... ";

    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        // Call Read before accessing data.
        while (reader.Read())
        {
            ReadSingleRow((IDataRecord)reader);
        }

        // Call Close when done reading.
        reader.Close();
    }

You should close the reader after while 过一会儿你应该关闭阅读器

public void add()
{
    var con = new DBConnection();
    try
    {
        for (int i = 0; i < listBServices.Items.Count; i++)
        {
            SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = '" +
                listBServices.Items.ToString() + "';", con.Connection);
            SqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())
            {
                int price = rd.GetInt32(0);
                listBPrice.Items.Add(price.ToString());
            }

            rd.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

listBServices.Items.ToString() results in the string "System.Windows.Forms.ListBox+ObjectCollection" . listBServices.Items.ToString()生成字符串"System.Windows.Forms.ListBox+ObjectCollection" You must use 您必须使用

SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = '" + 
                                listBServices.Items[i] + "'",
                                con.Connection);

But using string concatenation is not a good idea. 但是,使用字符串连接不是一个好主意。 Use parameters instead. 请改用参数。

SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = @svc",
                                con.Connection);
cmd.Parameters.Add("@svc", SqlDbType.NVarChar).Value = listBServices.Items[i];

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

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