日期时间的小数位数过多

发布于 2024-10-06 10:55:18 字数 1378 浏览 3 评论 0原文

我有以下代码:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
// set properties on  builder (omitted for brevity)

using (SqlConnection connection = new SqlConnection(builder.ToString()))
{
    connection.Open();
    using (SqlCommand command = connection.CreateCommand())
    {
        command.CommandType = System.Data.CommandType.Text;
        command.CommandText = "SELECT * FROM Employee WHERE WhenHire < @HireDate";
        SqlParameter hireDateParameter = new SqlParameter("@HireDate", DateTime.Now);
        command.Parameters.Add(hireDateParameter);
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                // do stuff
            }
        }
    }
}

在 SQL Profiler 中显示为:

exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
                   N'@HireDate datetime',
                   @HireDate='2010-12-06 11:43:23.573'

如何确定日期时间参数的精度?我见过小数点后有 7 位数字。

exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
               N'@HireDate datetime',
               @HireDate='2010-12-06 11:43:23.5733125'

在这种情况下,该语句将无法执行并出现以下错误:

消息 8114,级别 16,状态 1,第 1 行 将数据类型 varchar 转换为时出错 日期时间。

I have the following code:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
// set properties on  builder (omitted for brevity)

using (SqlConnection connection = new SqlConnection(builder.ToString()))
{
    connection.Open();
    using (SqlCommand command = connection.CreateCommand())
    {
        command.CommandType = System.Data.CommandType.Text;
        command.CommandText = "SELECT * FROM Employee WHERE WhenHire < @HireDate";
        SqlParameter hireDateParameter = new SqlParameter("@HireDate", DateTime.Now);
        command.Parameters.Add(hireDateParameter);
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                // do stuff
            }
        }
    }
}

Which shows up in SQL Profiler as:

exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
                   N'@HireDate datetime',
                   @HireDate='2010-12-06 11:43:23.573'

How is the precision of the datetime parameter determined? I've seen this have 7 digits after the decimal.

exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
               N'@HireDate datetime',
               @HireDate='2010-12-06 11:43:23.5733125'

In that case, the statement fails to execute with this error:

Msg 8114, Level 16, State 1, Line 1
Error converting data type varchar to
datetime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪我太投入 2024-10-13 10:55:18

根据 Microsoft (http://msdn.microsoft.com/en-us/library/ms187819.aspx),T-SQL DateTime 类型的精度为 0.000、0.003 或 0.007 毫秒。任何比这更精确的东西都可能导致该错误。

除非您确实需要精确到毫秒,否则我会给 DateTime.Now 一个格式字符串 - 类似于“yyyy-MM-dd HH:mm:ss” - 这将为您提供:

exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
                   N'@HireDate datetime',
                   @HireDate='2010-12-06 11:43:23'

.NET Framework 能够报告以毫秒为间隔,精确到小数点后七位,因此,如果源数据来自 DateTime.Now,则可能会传入最多七位小数。

Per Microsoft (http://msdn.microsoft.com/en-us/library/ms187819.aspx), the T-SQL DateTime type has an accuracy of .000, .003, or .007 milliseconds. Anything with more precision than that could cause that error.

Unless you really need accuracy down to the millisecond, I would give DateTime.Now a format string -- something like "yyyy-MM-dd HH:mm:ss" -- which would give you:

exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
                   N'@HireDate datetime',
                   @HireDate='2010-12-06 11:43:23'

The .NET Framework is able to report on millisecond intervals down to seven decimal points, so if your source data is coming from DateTime.Now, you could potentially get up to seven decimal points passed in.

北城半夏 2024-10-13 10:55:18

我建议确保您使用带有 DateTime 数据库类型的 SqlParameter - 尝试以下代码:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
// set properties on  builder (omitted for brevity)

using (SqlConnection connection = new SqlConnection(builder.ToString()))
using (SqlCommand command = new SqlCommand(connection))
{
    command.CommandType = System.Data.CommandType.Text;
    command.CommandText = "SELECT * FROM Employee WHERE WhenHire < @HireDate";

    // make sure to have a SqlDbType.DateTime parameter!
    SqlParameter hireDateParameter = new SqlParameter("@HireDate", SqlDbType.DateTime);
    hireDateParameter.Value = DateTime.Now;

    command.Parameters.Add(hireDateParameter);

    // don't open the connection too early - this is early enough here!
    connection.Open();

    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
           // do stuff
        }
    }
}

I would recommend making sure you use a SqlParameter with a DateTime db type - try this code:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
// set properties on  builder (omitted for brevity)

using (SqlConnection connection = new SqlConnection(builder.ToString()))
using (SqlCommand command = new SqlCommand(connection))
{
    command.CommandType = System.Data.CommandType.Text;
    command.CommandText = "SELECT * FROM Employee WHERE WhenHire < @HireDate";

    // make sure to have a SqlDbType.DateTime parameter!
    SqlParameter hireDateParameter = new SqlParameter("@HireDate", SqlDbType.DateTime);
    hireDateParameter.Value = DateTime.Now;

    command.Parameters.Add(hireDateParameter);

    // don't open the connection too early - this is early enough here!
    connection.Open();

    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
           // do stuff
        }
    }
}
烦人精 2024-10-13 10:55:18

http://msdn.microsoft.com/en-us/library/ms186724.aspx

微软还提供多种日期和时间数据类型。一种是 datetime2,它提高了精度和用户定义的小数秒精度。

他们给出了一个具有 7 位亚秒精度的示例 datetime2。

http://msdn.microsoft.com/en-us/library/ms186724.aspx

Also from microsoft there are multiple date and time data types. One is datetime2 which has increased accuracy and user-defined fractional second precision.

They give an example datetime2 with 7 digits of sub second precision.

一枫情书 2024-10-13 10:55:18

看来客户端机器有些奇怪。不确定它到底是什么,但是当我们转移到另一台机器时,它不再增加额外的精度。感谢所有的想法。

It looks like it was something strange with the client machine. Not sure exactly what it was, but when we moved to another machine, it no longer added that extra precision. Thanks for all the ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文