0

我有包含产品和价格列的数据库,我的列包含相同的产品但价格不同。

product   | price
productA  | 20
productA  | 40    
productB  | 50
productB  | 10

我有按钮、文本框和组合框,如果我在组合框示例 productA 中选择一个产品并单击按钮添加,我只想添加 ProductA。

private void button6_Click(object sender, EventArgs e)
        {
            dgvMain.Rows.Clear();
            List<string>[] detailList = a.mysqlSelect("select * from sales where product like '" + comboTotal.Text + "'");
            for (int i = 0; i < detailList.Length; i++)
            {
                dgvMain.Rows.Add(detailList[i][0], detailList[i][1], detailList[i][2], detailList[i][3]);
            }

            for (int i = 0; i < dgvMain.Rows.Count; i++)
            {
                DataGridViewRowHeaderCell cell = dgvMain.Rows[i].HeaderCell;
                cell.Value = (i + 1).ToString();
                dgvMain.Rows[i].HeaderCell = cell;
            }

            string conn = "server=localhost;user=root;password='';database=cashieringdb;";
            MySqlConnection myconn = new MySqlConnection(conn);
            string sql = "select sum(price group by" + comboTotal.Text + ") from sales";
            MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            txtSum.Text = dt.Rows[0][0] + "";
        }

我在这里的某个地方有一个例外:

string conn = "server=localhost;user=root;password='';database=cashieringdb;";
            MySqlConnection myconn = new MySqlConnection(conn);
            string sql = "select sum(price group by" + comboTotal.Text + ") from sales";
            MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            txtSum.Text = dt.Rows[0][0] + "";
4

0 回答 0