C#如何查询日期时间列 [英] C# how to query date time column
本文介绍了C#如何查询日期时间列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在查询一个表,该表的字段类型为DateTime
喜欢
I am quering a table which has a field which type is DateTime
Like
Select * from Table where column='"+dt"'"
dt在哪里
where dt is like
DateTime dt=Convert.ToDateTime(dateTimePicker1.Text);
当我执行它时,它给出了数据类型不匹配的错误
我也试过dt.ToString("dd-mm-yy")
dt.toShortDateString()
和toLongDateString()
但仍然出现相同的错误
谁能告诉我我错了?
数据库是access
when i execute it,it gives error that Datatype mismatch
I also tried dt.ToString("dd-mm-yy")
dt.toShortDateString()
and toLongDateString()
but still same error arising
Can any one tell where i m wrong?
Databse is access
推荐答案
数据库DateTime格式始终为yyyy-MM-dd hh:mm:ss-但Date Tiem的隐式ToString取决于您的语言环境:for在英国,对于美国的MM/dd/yyyy,它将使用dd/MM/yyyy.
相反,请使用参数化查询:
The database DateTime format is always yyyy-MM-dd hh:mm:ss - but the implicit ToString of Date Tiem depends on your locale: for teh UK it will use dd/MM/yyyy, for the US MM/dd/yyyy.
Instead, use a paramaterized query:
OleDbCommand cmd = new OldDbCommand("SELECT * FROM Table WHERE column=@DT", con);
cmd.Parameters.AddWithValue("@DT", dt);
失败的原因与程序和数据库中的区域性设置有关.
使用dt.ToString("s")
-可日期/时间模式 [ SqlParameter [ http://www.codinghorror .com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html [
The reason it fails has something to do with Culture settings in your program and the database.
Usedt.ToString("s")
- Sortable date/time pattern[^].
Or use SqlParameter[^] for that job.
Much more simple. Check this link http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html[^]
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
string sql = "SELECT email, passwd, login_id, full_name FROM members WHERE email = @email";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.Parameters.Add("@email", email);
OleDbDataReader reader = cmd.ExecuteReader();
这篇关于C#如何查询日期时间列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文