2001-01-01 00:00:00.000插入数据库而不是2000-12-31 23:59:59

时间:2011-03-29 20:19:09

标签: c# sql-server-2005 datetime

我有一个向数据库添加行的方法(sql server 2005)。它有问题,因为当我有一行UpdateDate 2000-12-31 23:59:59时,它会插入2001-01-01 00:00:00.000。可能吗?如果重要的话,环境文化是好的。这对我来说很神奇:/

private void AddInvestmentStatus(InvestmentData.StatusyInwestycjiRow investmentStatusesRow)
{
    SqlCommand cmd = new SqlCommand("AddInvestmentStatus");
    cmd.CommandType = CommandType.StoredProcedure;


    SqlParameter param1 = new SqlParameter("@InvestmentId", SqlDbType.BigInt);
    param1.Value = investmentStatusesRow.InvestmentId;
    cmd.Parameters.Add(param1);
    cmd.Parameters.AddWithValue("@enumInvestmentStatusID", investmentStatusesRow.EnumInvestmentStatusID);
    cmd.Parameters.AddWithValue("@modifiedBy", "System");
    cmd.Parameters.AddWithValue("@UpdateDate", investmentStatusesRow.UpdateDate);
    cmd.Parameters.AddWithValue("@ModifiedOn", investmentStatusesRow.ModifiedOn);
    cmd.Parameters.AddWithValue("@dataVersion", investmentStatusesRow.DataVersion);


    cmd.Connection = new SqlConnection(MyProgram.Properties.Settings.Default.ConnectionString);
    if (cmd.Connection.State != ConnectionState.Open)
        cmd.Connection.Open();

    try
    {
        cmd.ExecuteNonQuery();
    }
    catch (Exception e)
    {
        throw;
    }
}

}

create PROCEDURE [dbo].[AddInvestmentStatus] 
    @inwestmentID bigint,
    @enumInvestmentStatusId bigint,
    @updateDate datetime,
    @dataVersion int,
    @modifiedBy nvarchar(50),
    @modifiedOn datetime
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @investmentStatusesID bigint

    INSERT INTO StatusyInwestycji(InwestycjaID)
    VALUES (@inwestmentID)

    SELECT @investmentStatusesID = SCOPE_IDENTITY();

    INSERT INTO StatusyInwestycjiData(InvestmentStatusId, EnumStatusInwestycjiID,
            UpdateDate, DataVersion, ModifiedBy, ModifiedOn)
    VALUES (@investmentStatusesID, @enumInvestmentStatusId,
            @updateDate, @dataVersion, @modifiedBy, @modifiedOn)

END

编辑:

我的约会对象:

{2000-12-31 22:59:59}
    Date: {2000-12-31 00:00:00}
    Day: 31
    DayOfWeek: Sunday
    DayOfYear: 366
    Hour: 22
    Kind: Utc
    Millisecond: 999
    Minute: 59
    Month: 12
    Second: 59
    Ticks: 631139003999990000
    TimeOfDay: {22:59:59.9990000}
    Year: 2000

3 个答案:

答案 0 :(得分:10)

你确定你输入了23:59:59.000000或者你输入了23:59:59.9999999吗?

SQL DateTime数据类型的精度为3.33ms(它将舍入为0ms,3ms,7ms增量),这意味着您的23:59:59.9999将四舍五入到第二天的00:00:00.000000。

答案 1 :(得分:4)

我不知道你的情况是否会发生这种情况,但是一个已知的问题是,毫秒部分为.998或.999的日期时间值在插入数据库时​​会四舍五入到下一个整数秒,在最坏的情况下,可以将日期时间值包含在下一年。

答案 2 :(得分:3)

如果表有smalldatetime列,则可以,因为它具有1分钟的精度

见这里

select CONVERT(smalldatetime,'2000-12-31 23:59:59')
日期时间不会发生

select CONVERT(datetime,'2000-12-31 23:59:59')

发布表的DDL,确保还检查可能会转换为smalldatetime的触发器