3

您好,我正在使用此代码从 MySQL 数据库上的 5 个不同表中获取所需的数据。

private void goDateBtn_Click(object sender, EventArgs e)
        {
            reportList.Items.Clear();
            var db = new DBConnect();
            MySqlCommand cmd = null;
            MySqlDataReader dr = null;
            double totalsales = 0;
            try
            {
                if (db.OpenConnection() == true)
                {

                    string cmdstr = "SELECT ol.*, o.*, m.* "+
                                    "FROM orderlist ol "+
                                    "INNER JOIN orderdb o ON ol.order_ID = o.order_ID "+
                                    "INNER JOIN menudb m ON ol.menu_ID = m.menu_ID "+
                                    "INNER JOIN recipelist r ON r.menu_ID = m.menu_ID "+
                                    "INNER JOIN "+
                                    "( SELECT stock_ID, SUM(stock_pricePerPiece) menu_cost "+
                                        "FROM stocksdb "+
                                        "GROUP BY stock_ID )"+
                                    "s ON r.stock_ID = s.stock_ID "+
                                    "WHERE o.order_date >= #" + fromDate.Value.Date + "# AND " +
                                        "o.order_date <= #" + toDate.Value.Date + "#";
                    cmd = new MySqlCommand(cmdstr, db.mycon);
                    dr = cmd.ExecuteReader();
                    string[] info = new string[20];
                    while (dr.Read())
                    {
                        info[1] = (dr["order_ID"].ToString());
                        info[2] = (dr["order_date"].ToString());
                        info[3] = (dr["menu_name"].ToString());
                        info[4] = (dr["menu_cost"].ToString());
                        info[5] = (dr["menu_price"].ToString());
                        info[6] = (Convert.ToDouble(info[5]) - Convert.ToDouble(info[4])).ToString("#0.00");
                        this.reportList.Items.Add(new ListViewItem(new string[] { info[1], info[2], info[3], info[4], info[5], info[6] }));

                        totalsales += Convert.ToDouble(info[6]);
                    }
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
                db.CloseConnnection();
            }
            totalSalesTxtBox.Text = totalsales.ToString("#0.00");
            MessageBox.Show("SALES REPORT FINISHED!");
        }

我真的不知道我做错了什么,尤其是在 sql 字符串中,其中最后一个应该从表INNER JOIN中加起来,其中股票在同一个菜单上使用,因此在表中具有相同的值。stock_pricePerPiecestocksdbmenu_IDrecipedb

我得到了错误dr = cmd.ExecuteReader();

4

2 回答 2

2

我意识到这个问题的答案并不容易解释,但它与我试图通过命令字符串虚拟重新创建的列有关。我所做的以及解决问题的方法是将要检索的特定列放入SELECT查询中。

工作代码:

 private void goDateBtn_Click(object sender, EventArgs e)
        {
            reportList.Items.Clear();
            var db = new DBConnect();
            MySqlCommand cmd = null;
            MySqlDataReader dr = null;
            double totalsales = 0;
            try
            {
                if (db.OpenConnection() == true)
                {

                    string cmdstr = "SELECT DISTINCT ol.*, o.order_ID, o.order_date, o.order_status, m.*, mc.menu_cost " +
                                    "FROM  orderlist ol " +
                                        "INNER JOIN orderdb    o ON ol.order_ID = o.order_ID " +
                                        "INNER JOIN menudb     m ON ol.menu_ID  = m.menu_ID " +
                                        "INNER JOIN " +
                                            "(SELECT SUM(s.stock_pricePerPiece * r.recipe_quantity) AS menu_cost, m.menu_ID " +
                                                "FROM recipelist r " +
                                                    "INNER JOIN stocksdb s ON r.stock_ID = s.stock_ID " +
                                                    "INNER JOIN menudb m ON r.menu_ID= m.menu_ID " +
                                                "GROUP BY r.menu_ID " +
                                             ")" +
                                        "mc ON m.menu_ID = mc.menu_ID " +
                                    "WHERE o.order_date >= '" + fromDate.Value.Date.ToString("yyyy-MM-dd") + " 00:00:00" + "' AND " +
                                        "o.order_date <= '" + toDate.Value.Date.ToString("yyyy-MM-dd") + " 23:59:59" + "' AND " +
                                        "o.order_status = 'COMPLETED'";

                    if (menuCheckBox.Checked == true)
                    {
                        cmdstr = cmdstr + " AND m.menu_name = '" + menuBox.Text + "'";
                    }

                    cmd = new MySqlCommand(cmdstr, db.mycon);
                    dr = cmd.ExecuteReader();

                    string[] info = new string[20];
                    while (dr.Read())
                    {
                        info[1] = (dr["order_ID"].ToString());
                        info[2] = (dr["order_date"].ToString());
                        info[3] = (dr["menu_name"].ToString());
                        info[4] = (dr["order_quantity"].ToString());
                        info[5] = Convert.ToDouble(dr["menu_cost"].ToString()).ToString("#0.00");
                        info[6] = Convert.ToDouble(dr["menu_price"].ToString()).ToString("#0.00");
                        info[7] = ((Convert.ToDouble(dr["menu_price"].ToString()) * Convert.ToDouble(info[4])) - (Convert.ToDouble(dr["menu_cost"].ToString()) * Convert.ToDouble(info[4]))).ToString("#0.00");
                        this.reportList.Items.Add(new ListViewItem(new string[] { info[1], info[2], info[3], info[4], info[5], info[6], info[7] }));

                        totalsales += Convert.ToDouble(info[7]);
                    }
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
                db.CloseConnnection();
            }
            totalSalesTxtBox.Text = totalsales.ToString("#0.00");
            MessageBox.Show("SALES REPORT FINISHED!");
        }
于 2012-12-30T11:21:56.797 回答
1

根据您对问题的编辑,指出哪一行正在生成错误,这完全颠覆了我之前的答案。

如果您执行以下操作会发生什么?它可能有助于诊断您的查询语法:

Console.WriteLine(cmdstr);

我注意到的一件事是您在括号后需要一个空格:

"GROUP BY stock_ID )"+

认为这不会影响查询,但可能值得一试。除此之外,我建议复制上面生成的查询并将其手动粘贴到您最喜欢的 SQL 应用程序中,并确保其语法正确。

于 2012-12-28T07:19:35.463 回答