它没有在查询中显示正确的日期 [英] it is not showing correct date as per in query

查看:68
本文介绍了它没有在查询中显示正确的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  button3_Click(对象发​​件人,EventArgs e)
{
checkGrid = 到期;

dueDate DD = new dueDate();

DD.ShowDialog();

GlobalClass GC = new GlobalClass();

DateTime vdateFrom = GC.getDueDateFrom();
DateTime vdateTo = GC.getDueDateTo();

string getAllData = 选择P_Id,P_Password,格式(Treat_Due_Date,\'dd / MM / yyyy'\)作为DueDate,P_Name,P_City,来自patient_Details的medicineTaker,Treatment_Details where patient_Details.P_ID = Treatment_Details.Treat_P_ID AND Treat_Due_Date> =# + vdateFrom.ToString( dd / MM / yyyy)+ #AND Treat_Due_Date< =# + vdateTo.ToString( dd / MM / yyyy)+ ;

bindGrid(getAllData);

}

解决方案

首先不连接字符串以形成SQL命令 - 不仅通常是危险的(虽然不是在这种情况下)它也会导致问题,尤其是日期值。您应该 总是 使用参数化值。



问题是SQL不知道什么日期格式你传递它 - 所以它猜测,经常猜错了。将日期作为参数传递可以避免所有这些 - 以及更好的练习和更高效。

因此直接使用DateTime值,并将它们作为参数传递 - 它应该可以解决您的问题。

private void button3_Click(object sender, EventArgs e)
{
            checkGrid = "Due";

            dueDate DD = new dueDate();

            DD.ShowDialog();

            GlobalClass GC = new GlobalClass();

            DateTime vdateFrom = GC.getDueDateFrom();
            DateTime vdateTo = GC.getDueDateTo();

            string getAllData = "Select P_Id,P_Password,Format(Treat_Due_Date,\"'dd/MM/yyyy'\") As DueDate,P_Name,P_City,medicineTaker from patient_Details,Treatment_Details Where patient_Details.P_ID = Treatment_Details.Treat_P_ID AND Treat_Due_Date >= #" + vdateFrom.ToString("dd/MM/yyyy") + "# AND Treat_Due_Date <= #" + vdateTo.ToString("dd/MM/yyyy") + "#";

            bindGrid(getAllData);            
            
}

解决方案

Start by not concatenating strings to form your SQL command - not only is is generally dangerous (though not in this case) it also causes problems, particularly with date values. You should always use parameterized values instead.

The problem is that SQL doesn't not know what date format you are passing it - so it "guesses" and frequently it guesses wrong. Passing your dates as parameters avoids all that - as well as being better practice, and more efficient.
So use the DateTime values directly, and pass them as parameters - it should cure your problem.


这篇关于它没有在查询中显示正确的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆