您好,我正在使用此代码从 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();