在SqlParameter中使用DateTime存储过程,格式错误

我正在尝试使用DateTime作为SqlParameter的值从C#,.NET 2.0调用存储过程(在SQL 2005服务器上)。 存储过程中的SQL类型是“datetime”。

从SQL Management Studio执行sproc工作正常。 但每次我从C#调用它时都会收到有关日期格式的错误。

当我运行SQL事件探查器来监视调用时,我然后复制粘贴exec调用以查看正在发生的事情。 这些是我对我尝试过的观察和注释:

1)如果我直接将DateTime作为DateTime传递或转换为SqlDateTime ,则该字段由单引号的PAIR包围,例如

 @Date_Of_Birth=N''1/8/2009 8:06:17 PM'' 

2)如果我将DateTime作为字符串传递,我只得到单引号

3)使用SqlDateTime.ToSqlString()不会产生UTC格式的日期时间字符串(即使转换为通用时间后)

4)使用DateTime.ToString()不会产生UTC格式的日期时间字符串。

5)手动将SqlParameterDbType设置为DateTime不会更改上述观察结果。

那么,我的问题是,我究竟是如何让C#在SqlParameter传递正确格式化的时间? 当然这是一个常见的用例,为什么这么难以开始工作? 我似乎无法将DateTime转换为SQL兼容的字符串(例如’2009-01-08T08:22:45’)

编辑

RE:BFree,实际执行sproc的代码如下:

 using (SqlCommand sprocCommand = new SqlCommand(sprocName)) { sprocCommand.Connection = transaction.Connection; sprocCommand.Transaction = transaction; sprocCommand.CommandType = System.Data.CommandType.StoredProcedure; sprocCommand.Parameters.AddRange(parameters.ToArray()); sprocCommand.ExecuteNonQuery(); } 

详细了解我尝试过的内容:

 parameters.Add(new SqlParameter("@Date_Of_Birth", DOB)); parameters.Add(new SqlParameter("@Date_Of_Birth", DOB.ToUniversalTime())); parameters.Add(new SqlParameter("@Date_Of_Birth", DOB.ToUniversalTime().ToString())); SqlParameter param = new SqlParameter("@Date_Of_Birth", System.Data.SqlDbType.DateTime); param.Value = DOB.ToUniversalTime(); parameters.Add(param); SqlParameter param = new SqlParameter("@Date_Of_Birth", SqlDbType.DateTime); param.Value = new SqlDateTime(DOB.ToUniversalTime()); parameters.Add(param); parameters.Add(new SqlParameter("@Date_Of_Birth", new SqlDateTime(DOB.ToUniversalTime()).ToSqlString())); 

额外的编辑

我认为最有可能工作的那个:

 SqlParameter param = new SqlParameter("@Date_Of_Birth", System.Data.SqlDbType.DateTime); param.Value = DOB; 

在SQL事件探查器中看到的exec调用中的此值

 @Date_Of_Birth=''2009-01-08 15:08:21:813'' 

如果我将其修改为:

 @Date_Of_Birth='2009-01-08T15:08:21' 

它可以工作,但它不会用一对单引号进行解析,并且它不会正确转换为DateTime ,日期和时间之间的空格以及结尾的毫秒数。

更新和成功

我在下面的请求之后复制/粘贴了上面的代码。 我在这里和那里修剪的东西都很简洁。 事实certificate我的问题出现在我遗漏的代码中,我相信你们中的任何一个人都会在瞬间发现。 我把我的sproc调用包装在一个事务中。 事实certificate,我根本就没有做transaction.Commit()提交transaction.Commit() !!!!! 我很惭愧地说出来,但是你有它。

我仍然不知道我从探查器返回的语法是怎么回事。 一位同事用他自己的计算机实例观察了他的计算机,并返回了正确的语法。 从我的探查器中观察非常的SAME执行显示了错误的语法。 它充当了一个红鲱鱼,让我相信有一个查询语法问题,而不是更简单和真实的答案,这是我需要提交事务!

我将下面的答案标记为正确,并对其他人进行了一些投票,因为他们确实回答了这个问题,即使他们没有解决我的具体(脑力失误)问题。

你是如何设置SqlParameter ? 您应该将SqlDbType属性设置为SqlDbType.DateTime ,然后将DateTime直接传递给参数(不要转换为字符串,然后您要求一堆问题)。

您应该能够将值传入数据库。 如果没有,这是一个如何做到这一点的一个非常简单的例子:

 static void Main(string[] args) { // Create the connection. using (SqlConnection connection = new SqlConnection(@"Data Source=...")) { // Open the connection. connection.Open(); // Create the command. using (SqlCommand command = new SqlCommand("xsp_Test", connection)) { // Set the command type. command.CommandType = System.Data.CommandType.StoredProcedure; // Add the parameter. SqlParameter parameter = command.Parameters.Add("@dt", System.Data.SqlDbType.DateTime); // Set the value. parameter.Value = DateTime.Now; // Make the call. command.ExecuteNonQuery(); } } } 

我认为这里的部分问题是你担心时间是UTC的事实没有传达给SQL Server。 为此,您不应该这样做,因为SQL Server不知道特定时间是在特定的语言环境/时区。

如果要存储UTC值,则在将其传递给SQL Server之前将其转换为UTC(除非您的服务器与生成DateTime的客户端代码具有相同的时区,即便如此,这也是风险,IMO)。 SQL Server将存储此值,当您将其返回时,如果要在本地时间显示它,则必须自己执行( DateTime结构将很容易执行)。

所有这一切,如果您执行转换,然后将转换后的UTC日期(通过调用ToUniversalTime方法获得的日期,而不是通过转换为字符串)传递给存储过程。

当您获得值时,请调用ToLocalTime方法以获取本地时区的时间。

这是我添加参数的方式:

 sprocCommand.Parameters.Add(New SqlParameter("@Date_Of_Birth",Data.SqlDbType.DateTime)) sprocCommand.Parameters("@Date_Of_Birth").Value = DOB 

我假设当你写出DOB时没有引号。

您是否使用第三方控件来获取日期? 我对其中一些文本值的生成方式存在问题。

最后,如果您在不引用DOB的情况下键入参数的.Value属性,它是否有效?

只需使用:

  param.AddWithValue("@Date_Of_Birth",DOB); 

这将解决你所有的问题。

如果您使用Microsoft.ApplicationBlocks.Data它将使您的sprocs调用一行

 SqlHelper.ExecuteNonQuery(ConnectionString, "SprocName", DOB) 

哦,我认为casperOne是正确的…如果你想确保多个时区的正确日期时间,那么只需将值转换为UTC,然后再将值发送到SQL Server

 SqlHelper.ExecuteNonQuery(ConnectionString, "SprocName", DOB.ToUniversalTime())