使用多个组合框对多个表中的数据进行C#连接到SQL Server

时间:2018-11-25 07:03:21

标签: c# sql-server

我需要以下C#代码,将信息从SQL Server数据库传递到组合框和列表框。我可以使客户信息正常工作,但是一旦添加产品信息(粗体),就会出现错误。

我不确定如何编写选择查询,以便相关表中的信息将传递到必填的cb和lb字段?

private void frmSalesAdd_Load(object sender, EventArgs e)
{
     string selectQuery;

     selectQuery = "SELECT * FROM Customers";
     **selectQuery += "SELECT * FROM Products";**

     SqlConnection conn = ConnectionManager.DatabaseConnection();
     SqlDataReader rdr = null;

     try
     {
         conn.Open();

         SqlCommand cmd = new SqlCommand(selectQuery, conn);
         rdr = cmd.ExecuteReader();

         while (rdr.Read())
         {
             lbCustomerID.Items.Add(rdr["CustomerID"].ToString());
             cbCustomer.Items.Add(rdr["LastName" ].ToString());
             **lbProductID.Items.Add(rdr["ProductID"].ToString());
             cbProduct.Items.Add(rdr["Product"].ToString());**
         }

         rdr.Close();
    }
}

我得到的错误是:

  

UnsuccessfulSystem.Data.SqlClient.SqlException(0x80131904):“ *”附近的语法不正确,

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

ExecuteReader将执行一个有效的SQL命令。您无法将多个命令添加到SqlCommand并只能执行一次读取。

这应该有效:

private void frmSalesAdd_Load(object sender, EventArgs e)
{
    string selectQuery;

    selectCustomers = "SELECT * FROM Customers";
    selectProducts = "SELECT * FROM Products";

    SqlConnection conn = ConnectionManager.DatabaseConnection();

    conn.Open();

    SqlCommand cmd = new SqlCommand(selectCustomers, conn);

    using(SqlDataReader rdr = cmd.ExecuteReader()) 
    {
        while (rdr.Read())
        {
            lbCustomerID.Items.Add(rdr["CustomerID"].ToString());
            cbCustomer.Items.Add(rdr["LastName" ].ToString());
        }
    }

    cmd = new SqlCommand(selectProducts, conn);

    using(SqlDataReader rdr = cmd.ExecuteReader()) 
    {
        while (rdr.Read())
        {
            lbProductID.Items.Add(rdr["ProductID"].ToString());
            cbProduct.Items.Add(rdr["Product"].ToString());
        }
    }
}

答案 1 :(得分:0)

如果您真的想从查询中返回多个结果集,则还需要使用数据库阅读器读取它们。

尝试以下代码:

private void frmSalesAdd_Load(object sender, EventArgs e)
{
     // make sure to have a VALID SQL statement here!
     string selectQuery = "SELECT * FROM Customers; SELECT * FROM Products";

     SqlConnection conn = ConnectionManager.DatabaseConnection();

     try
     {
         conn.Open();

         SqlCommand cmd = new SqlCommand(selectQuery, conn);
         SqlDataReader rdr = cmd.ExecuteReader();

         // read the first result set - the return values from "SELECT * FROM Customers"
         while (rdr.Read())
         {
             lbCustomerID.Items.Add(rdr["CustomerID"].ToString());
             cbCustomer.Items.Add(rdr["LastName" ].ToString());
         }

         // when you're done reading the first result set - 
         // try to move ahead to the next result set
         if (rdr.NextResult())
         {
             // read the second result set - the return values from "SELECT * FROM Products"
             while (rdr.Read())
             {
                 lbProductID.Items.Add(rdr["ProductID"].ToString());
                 cbProduct.Items.Add(rdr["Product"].ToString());
             }
         }

         rdr.Close();
    }
}