当我选择From Date和Todate from datetimepicker来显示datagridview中的数据时,datagridview显示空数据,代码写入按钮点击evet,这里我使用访问数据库
DateTime startT = new DateTime();
DateTime endT = new DateTime();
startT = dateTimePicker1.Value.Date;
endT = dateTimePicker2.Value.Date;
if (startT.Date > endT.Date)
{
MessageBox.Show("To Date Cannot be greater than Start Date");
}
else
{
string connetionString = null;
connetionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
con.ConnectionString = connetionString;
DataSet ds = new DataSet();
string sql = "SELECT Medicine_name,sum(Medicine_count) as Medicine_count,To_Date from Medicine_count_info where [To_Date] Between #" + startT.ToString("dd'/'MM'/'yyyy") + "#And #" + endT.ToString("dd'/'MM'/'yyyy") + "#group by Medicine_name,Medicine_count,To_Date order by Medicine_count desc ";
OleDbConnection connection = new OleDbConnection(connetionString);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
ds = new DataSet();
connection.Open();
dataadapter.Fill(ds);
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
dataGridView1.Refresh();
}
答案 0 :(得分:0)
首先,在您的选择中,您错过了"#And #"
另外,假设[To_Date]是一个日期时间字段,问题可能就是如何构建将数据作为字符串传递的select子句。改为使用DateTime字段,当然也总是使用PARAMETRIZED QUERIES。像这样:
string sql = "SELECT Medicine_name,sum(Medicine_count) as Medicine_count,To_Date " +
"FROM Medicine_count_info " +
"WHERE [To_Date] Between @startDate And @endDate " +
"GROUP BY Medicine_name,Medicine_count,To_Date " +
"ORDER BY Medicine_count desc ";
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
dataadapter.SelectCommand.Parameters.AddWithValue("@startDate", startT);
dataadapter.SelectCommand.Parameters.AddWithValue("@startDate", endT);
ds = new DataSet();
dataadapter.Fill(ds);
...