问题描述
我在sql数据库中有一个数据类型为"date"的field(dt),在我的C#格式中,标签中有一个日期(例如07-11-2011).我想写一阵子条件来与日期进行比较在数据库中...类似........
i have a field(dt) in sql database with datatype "date" and in my c# form there is a date in a label( like 07-11-2011).i wanna write a while condition to compare with a date in database...something like........
string query = "select name from vacseat where dt='"+lblDate.Text+"'";
它不起作用,请帮助
its not working ,plz help
thanx in advance.
推荐答案
DateTime dt = DateTime.Today;
String query = "select name from vacseat where dt = @dt";
DbCommand cmd = new DbCommand(query, connectionObject); // Probably an SqlCommand or something.
cmd.Parameters.AddWithValue("@dt", dt); // Replaces @dt in your query with an actual date!
// Etc...
这应该可以解决问题:)
有关命令对象的更多信息,请参见此MSDN页面 [ ^ ].
祝你好运! :)
This should do the trick :)
For more info on Command Object see this MSDN page[^].
Good luck! :)
string query = "select name from vacseat where CONVERT(VARCHAR(10),dt,105)= '"+lblDate.Text+"'"; // Will format native SQL date time value to dd-mm-yyyy format
对此,Naerling关于SQL注入的建议对于数据库安全性非常有帮助.
主页,这将对您有帮助.
In addition to this what Naerling has suggest about SQL Injection is quite helpful for database safety.
Home this will help you well.
这篇关于将sql中的日期字段与c#label中的日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!