我需要通过使用2 datetimepicker fromdate和todate来过滤和查询数据当单击搜索按钮时,我尝试了以下代码,但不起作用:
private void BtnSearch_Click(object sender, EventArgs e)
{
string sql = @" SELECT [LAB_RESULTS].ORDER_ID as 'Order No.'
,labtests.TestId as 'Test Id'
,patients.Patient_No as 'Patient File No'
,Patients.Patient_Name as 'Patient Name'
,testname as 'Test Name'
,[RESULT_NUMBER] as 'Result'
,Machines.Machine_name as 'Machine'
,PatientCat.CatName as 'Category'
,departments.dept_name as 'Department'
,LAB_RESULTS.EXAMINED_BY as 'Examined By'
,LAB_RESULTS.EXAMINED_DATE as 'Examined Date'
,LAB_RESULTS.APPROVED_BY as 'Approved by'
,LAB_RESULTS.APPROVED_DATE as 'Approved Date'
,[RESULT_REPORT] as 'Report'
,[RESULT_NOTE] as 'Notes'
,[NORMAL_RESULT] as 'Normal'
,[UPDATED_BY]
,[UPDATED_DATE]
,REJECTED_BY
,REJECTED_DATE
,Lab_Reject_Reasons.Reject_Reason
FROM [dbo].[LAB_RESULTS]
inner join LabTests on LabTests.testid=LAB_RESULTS.TESTID
inner join Lab_orders_Cash on Lab_orders_Cash.cash_order_id = LAB_RESULTS.ORDER_ID
inner join Patients on Patients.Patient_No = Lab_orders_Cash.patient_no
inner join departments on LAB_RESULTS.deptid = departments.dept_id
inner join PatientCat on PatientCat.CatId = Lab_orders_Cash.catid
left join Machines on Machines.Machine_id = LAB_RESULTS.machine_id
left join Lab_Reject_Reasons on (LAB_RESULTS.REJECTED_REASON = Lab_Reject_Reasons.Reject_ID)
where [LAB_RESULTS].SAMPLE_STATUS in (5,6,8,10) ";
string condition = "";
string orderby = "";
DateTime fromDate ;
DateTime toDate ;
orderby += " order by LAB_RESULTS.ORDER_ID desc";
if (!DateTime.TryParse(dtFromDate.Text, out fromDate))
{
System.Windows.Forms.MessageBox.Show("Invalid From Date");
}
else if (!DateTime.TryParse(dtToDate.Text, out toDate))
{
System.Windows.Forms.MessageBox.Show("Invalid to Date");
}
else
{
condition += " and lab_orders_cash.order_date between '" + fromDate + " and '" + toDate + "'";
}
DataTable dt = data.fireDatatable(string.Format(sql + condition + orderby));
dgvResult.DataSource = dt;
dgvResult.Refresh();
}
我检查了此站点中的问题,但没有解决方案。
如何解决我的条件以获取两个日期之间的数据?
这是fireDatatable方法,它从数据库中读取数据:
public DataTable fireDatatable(string sql)
{
dt = new DataTable();
try
{
if (cn.State == ConnectionState.Closed) cn.Open();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
ad.SelectCommand = cmd;
ad.Fill(dt);
return dt;
}
catch
{
// error in sql or table
return null;
}
finally
{
// performance and speed
SqlConnection.ClearPool(cn);
cmd.Dispose();
if (cn.State == ConnectionState.Open) cn.Close();
}
}
else
{
condition += " and lab_orders_cash.order_date between '" + fromDate + "' and '" + toDate + "'";
}
然后看到运行时错误,就像Fauzi88先生所说的那样,尝试从方法中删除并捕获:
public DataTable fireDatatable(string sql) { dt = new DataTable(); if (cn.State == ConnectionState.Closed) cn.Open(); cmd.Connection = cn; cmd.CommandType = CommandType.Text; cmd.CommandText = sql; ad.SelectCommand = cmd; ad.Fill(dt); return dt; // performance and speed SqlConnection.ClearPool(cn); cmd.Dispose(); if (cn.State == ConnectionState.Open) cn.Close(); }