【问题标题】:How to add values to listbox from another listbox using SQL query?如何使用 SQL 查询从另一个列表框向列表框添加值?
【发布时间】:2019-02-10 15:23:06
【问题描述】:

用户单击btnAdd 并将项目传输到listBox1。现在我想创建一个查询,从 SQL 创建一个从listBox1SELECT FROM 的循环并将结果项添加到listBox2

我有这个示例代码,但它不起作用。有人可以帮我吗?

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);
    }
}

我得到了这个例外:

【问题讨论】:

    标签: c# sql-server winforms


    【解决方案1】:

    listBServices.Items.ToString() 生成字符串 "System.Windows.Forms.ListBox+ObjectCollection"。你必须使用

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

    但是使用字符串连接并不是一个好主意。改用参数。

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

    【讨论】:

    • 您好,感谢您的回答。但不幸的是,我得到 Specified cast is not valid 异常。
    • 参数类型必须与表中列的类型一致。例如。如果列是int(可能是服务ID),则必须使用SqlDbType.Int,并且必须将参数值指定为int。如果列表框包含服务名称,这将不起作用。如果它包含服务对象,则将其强制转换为该类型并获取 id:cmd.Parameters.Add("@svc", SqlDbType.Int).Value = ((ServiceClass)listBServices.Items[i]).ServiceID;。要将服务对象直接添加到 ListBox,请覆盖类中的 ToString 以使其显示服务名称。
    【解决方案2】:

    检查您的连接并使用使用代码块自动关闭连接。

     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();
        }
    

    【讨论】:

      【解决方案3】:

      你应该在一段时间后关闭阅读器

      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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-14
        • 1970-01-01
        • 1970-01-01
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多