c# - 如何自定义日期格式

标签 c# sql-server winforms sql-server-2005

我的表列之一名为“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-023/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/

相关文章:

时间:2019-01-17 标签:c#Windowsforms 'Object reference not set to an instance of an object'

c# - 使用 .NET XslCompiledTransform 类通过 Xslt 将 Xml 转换为有效的 Html 片段

c# - 通过索引设置treeview.SelectedNode (List<Int>)

c# - 将 WPF 事件映射到 Windows 窗体事件

c# - 我该怎么做 float :right in Windows Form Application?

c# - 无法通过 JavaScript 清除 ASP.net

c# - C# 引用的问题

sql-server - 我想在 SQL Server 2012 中优化查询

sql - where 子句与内连接语句检查,它们相同吗?

sql-server - 似乎无法让我的.Net Core应用程序在Docker容器中与SQL Server一起使用