从 VB.NET 解析日期以进行 SQL INSERT
我有一个带有表单的 Web 应用程序,我试图将其传递到 ASP.NET 服务器(使用 VB.NET),然后传递到 MS SQL Server 表。该表单在多个文本框中使用 jQuery 日期选择器,并将它们的格式设置为 MM/dd/yyyy
。然后,表单字段通过 PageMethod 传递到 Web 服务器,该服务器获取各种字段值并将它们组合成 SQL UPDATE 命令。
每当我尝试执行 SQL 命令时,我都会不断收到以下错误:
从字符串转换日期和/或时间时转换失败。
这是服务器上的代码:
Using myConn As New System.Data.SqlClient.SqlConnection(CString.ConnectionString)
myConn.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
"SET type = '" & type & "', " & _
"target = '" & "@target" & "', " & _
"patient = '" & patient & "', " & _
"dob = '" & "@dob" & "' " & _
"WHERE serial = '" & serial & "'", myConn)
cmd.Parameters.Add(SqlParameter("@target", Data.SqlDbType.Date))
cmd.Parameters.Add(SqlParameter("@dob", Data.SqlDbType.Date))
If target = "" Then
cmd.Parameters("@target").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("@target").Value = target
End If
If dob = "" Then
cmd.Parameters("@dob").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("@dob").Value = dob
End If
cmd.ExecuteNonQuery()
End Using
注意:我已经尝试过大约二十种不同的方法来解析日期,将它们转换为日期,改变格式,但没有一个起作用。
注 2:末尾的条件语句只是为了防止空日期字段在 SQL DB 中存储为“1/1/1900”,而是存储为实际的 SQL NULL 值。但从调试来看,这似乎不是问题——只有当存在实际值时才会引发错误。
如果有人能看到我做错了什么以及如何解决它,我将不胜感激。预先感谢您的帮助!
I have a web app with a form that I am trying to pass to an ASP.NET server (using VB.NET) and then on to a MS SQL Server table. The form uses a jQuery datepicker in several textboxes and formats them as MM/dd/yyyy
. The form fields are then passed through a PageMethod to the web server which takes the various field values and combines them into a SQL UPDATE command.
I am constantly getting the following error whenever I try to execute the SQL command:
Conversion failed when converting date and/or time from character string.
Here is the code on the server:
Using myConn As New System.Data.SqlClient.SqlConnection(CString.ConnectionString)
myConn.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
"SET type = '" & type & "', " & _
"target = '" & "@target" & "', " & _
"patient = '" & patient & "', " & _
"dob = '" & "@dob" & "' " & _
"WHERE serial = '" & serial & "'", myConn)
cmd.Parameters.Add(SqlParameter("@target", Data.SqlDbType.Date))
cmd.Parameters.Add(SqlParameter("@dob", Data.SqlDbType.Date))
If target = "" Then
cmd.Parameters("@target").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("@target").Value = target
End If
If dob = "" Then
cmd.Parameters("@dob").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("@dob").Value = dob
End If
cmd.ExecuteNonQuery()
End Using
Note: I've tried about twenty different ways of parsing the dates, converting them to dates, changing around the formats and none of it has worked.
Note 2: The conditional statements at the end are simply to prevent empty date fields from being stored in the SQL DB as "1/1/1900", but rather as an actual SQL NULL value. From debugging though, it seems that this is not the issue - it is when there is an actual value that the error is fired.
If anyone can see what I'm doing wrong and how I might fix it, it would be greatly appreciated. Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您混淆了参数化和非参数化部分(为什么不参数化所有内容?)
You are mixing up your parameterized and non-parameterized parts (why aren't you parameterizing everything?)
你包括时间吗?日期时间字段需要日期和时间。
Are you including time? DateTime fields require date and time.