我的表列之一名为“SetDate”,类型为 DateTime,如下所示:
2010-08-02 02:55:58.420
应用程序中的日期时间格式如下所示:
2/11/2010
问题是:
我正在通过应用程序将 SQL 查询传递到数据库。查询有 WHERE 子句 它将 SetDate 与来自应用程序的日期进行比较。
SetDate 列采用以下格式:2010-08-02 02:55:58.420
从应用程序传递的日期采用以下格式:3/12/2010
我只需要比较两个没有时间 2010-08-02
和 3/12/2010
的日期。
由于它们的格式不同,我没有从数据库中得到任何记录。
我使用 C# 和 T-SQL。
有什么想法吗?
最佳答案
您是否使用 SqlCommand 来运行查询?
是吗?还可以使用 SqlParameters 作为用户/系统输入。
var setDate = DateTime.Now();
using (SqlCommand command = new SqlCommand("SELECT * FROM TableX WHERE SetDate > @SetDate", connection))
{
// Add new SqlParameter to the command.
command.Parameters.Add(new SqlParameter("@SetDate", SqlDbType.DateTime, setDate));
// Read in the SELECT results.
SqlDataReader reader = command.ExecuteReader();
//More code
}
关于c# - 如何自定义日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3414804/