C# 使用datetimepicker c查询和筛选两个日期之间的数据

C# 使用datetimepicker c查询和筛选两个日期之间的数据,c#,.net,datetimepicker,C#,.net,Datetimepicker,我需要使用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' ,pa

我需要使用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();
            }
        }

我从聊天室得到了解决方案谢谢大家, 正如haldo先生所说,您首先添加了一句话:

else
            {
                condition += " and lab_orders_cash.order_date between '" + fromDate + "' and '" + toDate + "'";
            }
然后,为了在运行时看到错误,请从方法中删除try-and-catch,正如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();

        }

1您确定这些范围内的数据确实存在吗?2您能回答问题并添加sql变量的结果吗?sql的条件部分缺少一个引号。另外,你应该真正地参数化QueryIhave2DateTimePicker,我在一个网站上找到了这个解决方案,但它不起作用,我是c新手,当我使用select语句并在2019-12-01和2019-12-30之间使用order_date时,您能推荐我从sql server中的datetimepicker中选择日期的解决方案吗?它可以工作,我得到数据,但从屏幕上看,它不能使用此代码?如果看不到您的fireDataTable方法,就无法确定,但我非常确定,您需要为数据源适当设置日期格式。但是,最好使用参数化查询。不过,为了帮助解决这个问题,我们需要更多的信息。作为猜测,在创建条件时,尝试toDate.Value.Date.ToString yyy-MM-dd和fromDate。这里是“+fromDate+and”。在和之前应该有一个报价。它应该是“+fromDate+”和”