简体   繁体   English

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

[英]Parsing Date from VB.NET for SQL INSERT

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. 我有一个Web应用程序,该应用程序具有要传递给ASP.NET服务器(使用VB.NET)然后传递给MS SQL Server表的形式。 The form uses a jQuery datepicker in several textboxes and formats them as MM/dd/yyyy . 该表单在多个文本框中使用jQuery datepicker,并将其格式设置为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. 然后,表单字段通过PageMethod传递到Web服务器,该Web服务器采用各种字段值并将它们组合成SQL UPDATE命令。

I am constantly getting the following error whenever I try to execute the SQL command: 每当我尝试执行SQL命令时,都会不断出现以下错误:

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. 注意:我已经尝试了大约20种不同的解析日期,将其转换为日期,更改格式的方式,但没有一种有效。

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. 注意2:结尾处的条件语句只是为了防止将空日期字段存储为“ 1/1/1900”,而是存储为实际的SQL NULL值。 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! 在此先感谢您的帮助!

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)

Are you including time? 你有时间吗? DateTime fields require date and time. DateTime字段需要日期和时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM