从 VB.NET 解析日期以进行 SQL INSERT

发布于 2025-01-01 13:31:23 字数 1619 浏览 1 评论 0原文

我有一个带有表单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓酥 2025-01-08 13:31:23

您混淆了参数化和非参数化部分(为什么不参数化所有内容?)

Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
                         "SET type = '" & type & "', " & _ 
                             "target = @target, " & _
                             "patient = '" & patient & "', " & _
                             "dob = @dob " & _   
                         "WHERE serial = '" & serial & "'", myConn)

You are mixing up your parameterized and non-parameterized parts (why aren't you parameterizing everything?)

Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
                         "SET type = '" & type & "', " & _ 
                             "target = @target, " & _
                             "patient = '" & patient & "', " & _
                             "dob = @dob " & _   
                         "WHERE serial = '" & serial & "'", myConn)
无戏配角 2025-01-08 13:31:23

你包括时间吗?日期时间字段需要日期和时间。

Are you including time? DateTime fields require date and time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文