我是C#的新手,我从不存储来自mysql查询的对象。我已经声明了2个Datetimes变量:
DateTime date_min
DateTime date_max
我想从下面的查询中存储max(date)和min(date)
string query1 = "SELECT MIN(order_status.BEGIN_DATE) AS 'BEGIN DATE', MAX(order_status.END_DATE) AS 'END DATE' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE orders.NUMBER_ORDER = 'TEST';";
like:max(date) = MAX(order_status.END_DATE)
min(date) = MIN(order_status.BEGIN_DATE)
我要在此循环中使用的:
for (DateTime date = date_min; date <= date_max; date = date.AddDays(1))
{
string query2 = "SELECT COUNT(leave.ID_PRACOWNIKA) AS 'NUMBER OF WORKERS ON LEAVE' FROM project1.leave WHERE DATE(leave.BEGIN_DATE) <= '"+date+"' AND DATE(leave.END_DATE) >= '"+date+"';";
string query3 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ALL ORDERS IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"');";
string query4 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ORDER IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"') AND orders.NUMBER_ORDER = 'TEST';";
}
有人可以显示我应该写什么样的代码行吗?也许cmd.Parameters够了吗? THX寻求帮助。
有我的代码:
public partial class GenerateChartsOfOrders : Form
{
DateTime date_min;
DateTime date_max;
public GenerateChartsOfOrders()
{
InitializeComponent();
}
public void loaddata2()
{
string query1 = "SELECT MIN(order_status.BEGIN_DATE) AS 'BEGIN DATE', MAX(order_status.END_DATE) AS 'END DATE' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE orders.NUMBER_ORDER = 'TEST';";
for (DateTime date = date_min; date <= date_max; date = date.AddDays(1))
{
string query2 = "SELECT COUNT(leave.ID_PRACOWNIKA) AS 'NUMBER OF WORKERS ON LEAVE' FROM project1.leave WHERE DATE(leave.BEGIN_DATE) <= '"+date+"' AND DATE(leave.END_DATE) >= '"+date+"';";
string query3 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ALL ORDERS IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"');";
string query4 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ORDER IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"') AND orders.NUMBER_ORDER = 'TEST';";
}
}
}
答案 0 :(得分:0)
您可以使用ExecuteReader检索以下值,
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using(var command = new MySqlCommand(query1, con))
{
using(var reader = command.ExecuteReader())
{
while (reader.Read())
{
date_min = Convert.ToDateTime(reader[0]);
date_max = Convert.ToDateTime(reader[1]);
}
}
}
}
希望有帮助。