1

我有 2ListBoxes和 5 textBox。在ListBox_prod我想检索所有产品(来自 PRODUCT 表)并且Listbox_item我想在表单加载事件中检索与所选产品(来自 PRODITEM 表)相对应的所有项目。所有产品都有超过 1 个与之关联的项目。问题在于Listbox_item它只显示 1 个项目。但是我希望在Listbox_item选择特定产品时显示所有项目。 getData()在课堂上DBCommands实际上导致了问题。这是我的代码:

public partial class form_prodItems : Form
{       
       private DBCommands dBCommand;

     public form_prodItems()
    {
        InitializeComponent();
        listBx_prod.SelectedIndexChanged += new EventHandler(listBx_prod_selValChange);
        listBx_item.SelectedIndexChanged += new EventHandler(listBx_item_selValChange);
    }
   private void form_prodItems_Load(object sender, EventArgs e)
     { 
        // ...
        refresh_listBx_prod("PRODUCT", this.listBx_prod, null, null);
        refresh_listBx_prod("PRODITEM", this.listBx_item, listBx_prod.ValueMember,   listBx_prod.SelectedValue.ToString());
    }

    private void listBx_item_selValChange(object sender, EventArgs e) // causing problem
    {
        if (listBx_item.SelectedValue != null)
            showPrice("PRODITEM", listBx_item.ValueMember, listBx_item.SelectedValue.ToString());
    }

    private void listBx_prod_selValChange(object sender, EventArgs e)
    {
        if(listBx_prod.SelectedValue != null)
           refresh_listBx_prod("PRODITEM", this.listBx_item, listBx_prod.ValueMember, listBx_prod.SelectedValue.ToString());
    }

    private void showPrice(string tblName,string where_column ,string where_val)
    {
        DataSet ds;
        ds = dBCommand.getData("select * from " + tblName + " WHERE " + where_column + " = '" + where_val + "'");
        DataRow col_val = ds.Tables[0].Rows[0];

        txtBox_12oz.Text = col_val.ItemArray[3].ToString();
        txtBox_16oz.Text = col_val.ItemArray[4].ToString();
        txtBox_20oz.Text = col_val.ItemArray[5].ToString();
        txtBox_1lbs.Text = col_val.ItemArray[6].ToString();
        txtBox_2lbs.Text = col_val.ItemArray[7].ToString();
        //ds.Clear();
    }

    private void refresh_listBx_prod(string tblName, ListBox listBox, string where_column, string where_val)
    {
        DataSet ds = new DataSet();
        dBCommand = new DBCommands();

        if (where_column == null)
        {
            ds = dBCommand.getData("SELECT * FROM " + tblName);
        }
        else
        {
            ds = dBCommand.getData("SELECT * FROM " + tblName + " WHERE " + where_column + " = " + where_val);
        }
        listBox.DataSource = ds.Tables[0];
     //   ds.Clear();
    }
}   


public class DBCommands
{
    private SqlConnection conn;
    private SqlDataAdapter dataAdapter;
    private DataSet container;

    public DataSet getData(string selectCmd)
    {
        container.Clear();    // I guess something needs to be fixed here some where..
        conn = getConnection();
        dataAdapter.SelectCommand = new SqlCommand(selectCmd, conn);
        dataAdapter.Fill(container);
        conn.Close();

       return container;
    }
    private SqlConnection getConnection()
    {
        SqlConnection retConn = new SqlConnection("Data Source=" + Environment.MachineName + "; Initial Catalog=RESTAURANT; Integrated Security = TRUE");
        retConn.Open();
        return retConn;
    }
}

实际上,数据集会刷新它从 (SELECT * FROM PRODITEM where PRODUCT_id = '1') 获得的所有数据,并显示上次执行的查询中的数据,即 (select * from proditem where item_id = 1) 任何建议..

4

1 回答 1

0

我的建议是尝试通过SqlCommand. 这样你就可以更好地了解你得到了什么。使用这个例子:

    SqlCommand command = new SqlCommand(selectCmd, conn);
    SqlDataReader reader = command.ExecuteReader();
    try
    {
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }
    }
    finally
    {
        // Always call Close when done reading.
        reader.Close();
    }

并修改它,以便您根据需要返回一个表。我提供给您的原因是它易于使用,并且您可以立即获得调试信息,帮助您了解 sql 错误

于 2013-07-21T06:15:36.417 回答