使用 datetimepicker c# 查询和过滤 2 个日期之间的数据
Query and filter Data Between 2 Dates using datetimepicker c#
我需要使用 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 先生所说,从方法中删除 try 和 catch :
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();
}
我需要使用 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 先生所说,从方法中删除 try 和 catch :
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();
}