如何将 TimeSpan 添加到 SQL 服务器中的时间列
How do I add a TimeSpan to a time column in SQL Server
我想做如下事情:
update AutoTagUse set TimeActive = (TimeActive + @OrigTimeActive) where AutoTagUseId = @AutoTagUseId
其中 TimeActive
是 SQL 服务器中的一个 TIME(0)
列,OrigTimeActive
是一个 TimeSpan
变量(在 C# 中)。
我正在尝试这样做,这样我就可以让多个服务同时更新数据库中的这个值,而不必用事务来锁定事情。
创建列的 SQL 服务器条目是:
[TimeActive] TIME (0)
CONSTRAINT [DF_AutoTagUse_TimeActive] DEFAULT ('00:00:00') NOT NULL,
我现在正在尝试的是:
TimeSpan difference = TimeActive - origTimeActive;
conn.ExecuteScalar("update AutoTagUse set TimeActive = dateadd(SECOND, @OrigTimeActive, TimeActive) where AutoTagUseId = @AutoTagUseId",
new { AutoTagUseId = AutoTagUseId, OrigTimeActive = difference }, transaction);
我得到:
System.Data.SqlClient.SqlException
HResult=0x80131904
Message=Argument data type time is invalid for argument 2 of dateadd function.
Source=.Net SqlClient Data Provider
StackTrace:
at Dapper.SqlMapper.ExecuteScalarImpl[T](IDbConnection cnn, CommandDefinition& command)
at Dapper.SqlMapper.ExecuteScalar(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable
1 commandType)
at LicenseLibrary.DataObjects.AutoTagUse.Update(IDbConnection conn, IDbTransaction transaction) in C:\git\Store\LicenseLibrary\DataObjects\AutoTagUse.cs:line 171
at TestLibrary.DataObjects.TestAutoTagUse.<>c.b__2_0(IDbConnection conn) in C:\git\Store\TestLibrary\DataObjects\TestAutoTagUse.cs:line 59
at LicenseLibrary.Database.AzureDataAccess.<>c__DisplayClass11_0.b__0() in C:\git\Store\LicenseLibrary\Database\AzureDataAccess.cs:line 104
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.<>c__DisplayClass1.b__0()
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.ExecuteAction[TResult](Func`1 func)
作为时间 (0),我假设您的时间间隔是 SECONDS
您可以使用 DATEADD()
例子
Update AutoTagUse
set TimeActive = dateadd(SECOND,@OrigTimeActive,TimeActive)
Where AutoTagUseId = @AutoTagUseId
一般来说,如果您想存储时间 span,SQL 服务器中的 time
数据类型不适合。因为它是为存储 一天中的时间 而设计的,而不是时间跨度。这意味着不支持将这些值中的两个值相加(将 4pm 和 8pm 相加是没有意义的)。也不支持负值或大于 24 小时的值。
出于所有这些原因,我通常建议改用数字数据类型,并在其名称中明确指示预期的单位。 (我通常更喜欢所需的最小粒度的 int
类型,其他人可能会选择使用某种 decimal
类型)
例如我将使用 SecondsActive int
作为此处的列。
我想做如下事情:
update AutoTagUse set TimeActive = (TimeActive + @OrigTimeActive) where AutoTagUseId = @AutoTagUseId
其中 TimeActive
是 SQL 服务器中的一个 TIME(0)
列,OrigTimeActive
是一个 TimeSpan
变量(在 C# 中)。
我正在尝试这样做,这样我就可以让多个服务同时更新数据库中的这个值,而不必用事务来锁定事情。
创建列的 SQL 服务器条目是:
[TimeActive] TIME (0)
CONSTRAINT [DF_AutoTagUse_TimeActive] DEFAULT ('00:00:00') NOT NULL,
我现在正在尝试的是:
TimeSpan difference = TimeActive - origTimeActive;
conn.ExecuteScalar("update AutoTagUse set TimeActive = dateadd(SECOND, @OrigTimeActive, TimeActive) where AutoTagUseId = @AutoTagUseId",
new { AutoTagUseId = AutoTagUseId, OrigTimeActive = difference }, transaction);
我得到:
System.Data.SqlClient.SqlException
HResult=0x80131904
Message=Argument data type time is invalid for argument 2 of dateadd function.
Source=.Net SqlClient Data ProviderStackTrace:
at Dapper.SqlMapper.ExecuteScalarImpl[T](IDbConnection cnn, CommandDefinition& command)
at Dapper.SqlMapper.ExecuteScalar(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable
1 commandType)
at LicenseLibrary.DataObjects.AutoTagUse.Update(IDbConnection conn, IDbTransaction transaction) in C:\git\Store\LicenseLibrary\DataObjects\AutoTagUse.cs:line 171
at TestLibrary.DataObjects.TestAutoTagUse.<>c.b__2_0(IDbConnection conn) in C:\git\Store\TestLibrary\DataObjects\TestAutoTagUse.cs:line 59
at LicenseLibrary.Database.AzureDataAccess.<>c__DisplayClass11_0.b__0() in C:\git\Store\LicenseLibrary\Database\AzureDataAccess.cs:line 104
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.<>c__DisplayClass1.b__0()
at Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy.ExecuteAction[TResult](Func`1 func)
作为时间 (0),我假设您的时间间隔是 SECONDS
您可以使用 DATEADD()
例子
Update AutoTagUse
set TimeActive = dateadd(SECOND,@OrigTimeActive,TimeActive)
Where AutoTagUseId = @AutoTagUseId
一般来说,如果您想存储时间 span,SQL 服务器中的 time
数据类型不适合。因为它是为存储 一天中的时间 而设计的,而不是时间跨度。这意味着不支持将这些值中的两个值相加(将 4pm 和 8pm 相加是没有意义的)。也不支持负值或大于 24 小时的值。
出于所有这些原因,我通常建议改用数字数据类型,并在其名称中明确指示预期的单位。 (我通常更喜欢所需的最小粒度的 int
类型,其他人可能会选择使用某种 decimal
类型)
例如我将使用 SecondsActive int
作为此处的列。