日期时间有太多的小数位 [英] Datetime has too many decimal places

查看:215
本文介绍了日期时间有太多的小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

  SqlConnectionStringBuilder建设者=新SqlConnectionStringBuilder();
//对建设者的属性集(不再赘述)

使用(SqlConnection的连接=新的SqlConnection(builder.ToString()))
{
    connection.Open();
    使用(SqlCommand的命令= connection.CreateCommand())
    {
        command.CommandType = System.Data.CommandType.Text;
        command.CommandText =SELECT * FROM员工WHERE WhenHire< @HireDate;
        的SqlParameter hireDateParameter =新的SqlParameter(@雇用日期,DateTime.Now);
        command.Parameters.Add(hireDateParameter);
        使用(SqlDataReader的读卡器= Command.ExecuteReader却())
        {
            而(reader.Read())
            {
                //做的东西
            }
        }
    }
}
 

这在SQL事件探查器显示为:

  EXEC sp_executesql的N'SELECT * FROM员工WHERE WhenHire< @聘用日期',
                   N'@雇用日期日期时间',
                   @雇用日期='2010-12-06 11:43:23.573
 

如何日期时间参数precision确定的?我已经看到了这个有7小数点后的数字。

  EXEC sp_executesql的N'SELECT * FROM员工WHERE WhenHire< @聘用日期',
               N'@雇用日期日期时间',
               @雇用日期='2010-12-06 11:43:23.5733125
 

在这种情况下,该语句失败,此错误执行:

  

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

解决方案

看起来这是在2008年的事件探查器中的错误:

<一个href="http://blogs.msdn.com/b/sqlserverfaq/archive/2009/08/13/sql-server-2008-profiler-pads-the-datetime-datatype-with-0s-when-tracing-for-a-ado-net-application.aspx" rel="nofollow">http://blogs.msdn.com/b/sqlserverfaq/archive/2009/08/13/sql-server-2008-profiler-pads-the-datetime-datatype-with-0s-when-tracing-for-a-ado-net-application.aspx

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.

解决方案

Looks like this is a bug in the 2008 Profiler:

http://blogs.msdn.com/b/sqlserverfaq/archive/2009/08/13/sql-server-2008-profiler-pads-the-datetime-datatype-with-0s-when-tracing-for-a-ado-net-application.aspx

这篇关于日期时间有太多的小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆