从字符串转换datetime时转换失败

时间:2009-07-11 20:20:08

标签: asp.net vb.net sql-server-2005 sql-date-functions

传递给我的自定义函数的字符串如下:

SELECT key FROM ubis WHERE MemberID = '144'
AND To >='11/7/2009 9:11:23 pm'
AND From <= '11/7/2009 9:11:23 pm'

    Public Shared Function GetDataTable(ByVal CmdText As String) As DataTable
        Dim myConn As New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)
        Dim myCmd As New SqlCommand(CmdText, myConn)
        myConn.Open()
        Dim myReader As SqlDataReader = myCmd.ExecuteReader()
        Dim myTable As New DataTable()
        myTable.Load(myReader)
        myConn.Close()
        Return (myTable)
    End Function

这是我得到的错误, 从字符串

转换datetime时转换失败

我知道datetime字段作为字符串传递给函数,但我有哪些选项?

5 个答案:

答案 0 :(得分:2)

您是否考虑过使用参数化查询?这将解决您的问题,并在从用户输入中获取WHERE条件的情况下提高安全性。

示例(VB.NET):

Dim myCmd As New SqlCommand(CmdText, myConn)
myCmd.Parameters.AddWithValue("MemberID", 144)
myCmd.Parameters.AddWithValue("Timestamp", DateTime.Now)

与此查询文本(SQL)一起使用:

SELECT key FROM ubis WHERE MemberID = @MemberID
AND @Timestamp BETWEEN From AND To

偏离主题:SQL中的BETWEEN关键字只是表达>= AND <=条件的一种巧妙方式。

答案 1 :(得分:2)

这有一个sql注入漏洞的气味。那个日期没有任何机会来自用户(甚至间接),做到了吗?即使这个是安全的,像'GetDataTable()'这样没有考虑好查询参数的泛型函数几乎总是一个错误。

你想要更像这样的东西:

Public Shared Function GetMemberKeys(ByVal MemberID As Integer, ByVal KeyDate As DateTime) As DataTable

    Static sql As String= _
         "SELECT key" _
      + " FROM ubis" _ 
      + " WHERE MemberID= @MemberID AND @KeyDate BETWEEN [FROM] AND [TO]"

    Dim dt As New DataTable()
    Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString), _
          cmd As New SqlCommand(sql, cn)

        cmd.Parameters.Add("@MemberID", SqlDbType.Int).Value = MemberID
        cmd.Parameters.Add("@KeyDate", SqlDbType.DateTime).Value = KeyDate

        cn.Open()
        Using rdr As SqlDataReader = cmd.ExecuteReader()
             dt.Load(rdr)
        End Using
    End Using
    Return dt
End Function

答案 2 :(得分:1)

2009年7月7日是不明确的 - 是7月11日还是11月7日?

SQL无法分辨 - 它取决于它已经设置的默认值。最好以明确的格式传递日期:

SELECT key FROM ubis WHERE MemberID = '144' 
               AND To >='11 July 2009 9:11:23 pm' 
               AND From <= '11 July 2009 9:11:23 pm'

或者,按照Zyphrax的建议,使用正确的format codecustom one进行正确的转换:

SELECT key FROM ubis WHERE MemberID = '144'
       AND To >= CONVERT(datetime, '11/7/2009 9:11:23 pm', 105)
       AND From <= CONVERT(datetime, '11/7/2009 9:11:23 pm', 105)

答案 3 :(得分:1)

您是否尝试在管理工作室中运行sql并查看会发生什么?

答案 4 :(得分:0)

您可以使用CONVERT命令将char转换为日期时间。

SELECT key FROM ubis WHERE MemberID = '144'
           AND To >= CONVERT(datetime, '11/7/2009 9:11:23 pm', 105)
           AND From <= CONVERT(datetime, '11/7/2009 9:11:23 pm', 105)

我不确定105,您可能需要Google才能获得正确的格式代码。

此外,如果您的SQL代码遇到异常,则不会关闭您的连接。您可能需要添加一些使用代码来解决此问题。

Public Shared Function GetDataTable(ByVal CmdText As String) As DataTable        
 Using myConn As New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)
   Using myCmd As New SqlCommand(CmdTxt, myConn)
     conn.Open()
       Using myReader As SqlDataReader = myCmd.ExecuteReader()
           Dim myTable As New DataTable()
           myTable.Load(myReader)
           myConn.Close()
           Return (myTable)  
      End Using
     End Using
End Function