如何解决“索引超出数组范围”的错误?

时间:2012-12-28 07:12:19

标签: c# mysql sql indexing

您好我正在使用此代码从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_pricePerPiece表中的所有stocksdb加起来,其中股票是在同一菜单上使用,因此在menu_ID表中具有相同的recipedb

我收到dr = cmd.ExecuteReader();

上的错误

2 个答案:

答案 0 :(得分: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!");
        }

答案 1 :(得分:1)

完全根据您对问题的编辑消除我之前的答案,注意哪一行产生错误。

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

Console.WriteLine(cmdstr);

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

"GROUP BY stock_ID )"+

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