答案 0 :(得分:0)
使用DateTime.Now
更新语句:
SqlCommand cmd = new SqlCommand("Update yourTableName set TranTime = @dt",yourConnection);
SqlParameter parameter = cmd.Parameters.Add("@dt", System.Data.SqlDbType.DateTime);
parameter.Value = DateTime.Now;
如果您想按时间更新并且唯一,则可以使用TimeOfDay
。像这样:
DateTime.Now.TimeOfDay
或ToLongTimeString
喜欢这样:
DateTime.Now.ToLongTimeString()
根据您的需要。
答案 1 :(得分:0)
我提出了这个解决方案
DECLARE @ID INT
DECLARE @T int
DECLARE @Time time(7)
SET @ID = 2
set @T = 80005836 //Around 8:AM
WHILE (@ID <=15) //I want to update 15 rows only
BEGIN
SELECT (@T / 1000000) % 100 as hour,
(@T / 10000) % 100 as minute,
(@T / 100) % 100 as second,
(@T % 100) * 10 as millisecond
SET @Time =
(
SELECT dateadd(hour, (@T / 1000000) % 100,
dateadd(minute, (@T / 10000) % 100,
dateadd(second, (@T / 100) % 100,
dateadd(millisecond, (@T % 100) * 10, cast('00:00:00' as time(2)))))) AS Time
)
UPDATE myTable
SET TranTime = @Time
WHERE ID = @ID
//increment ctr for ID and add 1 hour for a Time
SET @ID = @ID + 1
SET @T = @T + 1000000
END
GO