如何使用datetimepicker从数据库中获取记录,其中插入日期的数据类型是date [英] How to fetch record from database using datetimepicker where data type of inserted date is date

查看:378
本文介绍了如何使用datetimepicker从数据库中获取记录,其中插入日期的数据类型是date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我选择确切日期时,在数据库中显示结果的时间,但是当选择不在db中的日期时,它会显示错误。



我尝试过:



Whenever I am selecting exact date which in database that time showing result but when choose date which is not in db that time it shows an error.

What I have tried:

cn.Open();
string getdate =dateTimePicker1.Value.ToString("dd-MM-yyyy");
SqlDataAdapter da = new SqlDataAdapter("select * from tbl_vehicle_pur_Mast where veh_purchase_date= '" + getdate + "' ", cn);
DataTable dt = new DataTable();
da.Fill((dt));
dataGridView1.DataSource = dt;

推荐答案

不要这样做。

使用参数化查询,并直接传递DateTime值:

Don't do that.
Use a parameterised query, and pass the DateTime value directly:
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tbl_vehicle_pur_Mast WHERE veh_purchase_date= @PD", cn);
da.SelectCommand.Parameters.AddWithValue("@PD", dateTimePicker1.Value.Date);
DataTable dt = new DataTable();
da.Fill((dt));
dataGridView1.DataSource = dt;


这篇关于如何使用datetimepicker从数据库中获取记录,其中插入日期的数据类型是date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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