-3

这是我希望将“2013-06-25 18:46:54.687”传递给 sql server 的日期时间格式。如何在 C# 中转换?

 DateTime LastUpdateTime=Convert.toDateTime(LastUpdateTime);
  //2013-06-25 18:46:54.687 with 3 index of millisecond
 sc.Parameters.Add("@LastUpdateTime", SqlDbType.DateTime).Value = LastUpdateTime;
4

2 回答 2

2

您在 SQL 中使用了错误的数据类型。

datetime2可以被认为是现有 datetime 类型的扩展,它具有更大的默认小数精度和可选的用户指定精度。


C# 格式毫秒正是你想要的方式。在一个例子中:

DateTime date2 = new DateTime(2008, 1, 1, 0, 30, 45, 125);
Console.WriteLine("Date: {0:o}", 
                  date2);           
// Displays the following output to the console:
//      Date: 2008-01-01T00:30:45.1250000

看看为什么 SQL Server 会损失一毫秒?DateTime2 与 SQL Server 中的 DateTime

顺便说一句,这些都是很好的问题,有很好的答案。

于 2013-06-25T10:58:12.693 回答
0

If your database query parameter is string, use following format:

LastUpdateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")

Otherwise, it would be better to send datetime as original object, and let sql server do all conversions.

于 2013-06-25T10:55:44.700 回答