使用日期作为搜索条件时VB出错

时间:2014-06-01 07:53:12

标签: sql vb.net date

获取此错误

  

SqlParameterCollection只接受非空的SqlParameter类型对象,而不接受SqlParameter []对象

尝试搜索日期

的记录时
Public Function GetSelectedLog(Optional ByVal FileNumber As String = Nothing, _
                               Optional ByVal dateFrom As Date = Nothing, _
                               Optional ByVal dateTo As Date = Nothing) As DataView
    Try
        Dim cmd As New SqlCommand("GetSelectedLog", objCommon.sqlCon)
        Dim adp As SqlDataAdapter
        adp = New SqlDataAdapter(cmd)

        Dim ds As New DataSet
        cmd.CommandType = CommandType.StoredProcedure
        Dim sqlParam(2) As SqlParameter

        sqlParam(0) = New SqlParameter("@FileNumber", IIf(FileNumber Is Nothing, DBNull.Value, FileNumber))
        If dateFrom <> DateTime.MinValue Then
            sqlParam(1) = New SqlParameter("@dateFrom", dateFrom)
        Else
            sqlParam(1) = New SqlParameter("@dateFrom", DBNull.Value)
        End If
        If dateTo <> DateTime.MinValue Then
            sqlParam(2) = New SqlParameter("@dateTo", dateTo)
        Else
            sqlParam(2) = New SqlParameter("@dateTo", DBNull.Value)
        End If

        cmd.Parameters.Add(sqlParam)

        If objCommon.sqlCon.State = ConnectionState.Closed Then objCommon.sqlCon.Open()
        adp.Fill(ds)
        Return ds.Tables(0).DefaultView
    Catch ex As Exception
        Return Nothing
    Finally
        objCommon.sqlCon.Close()
    End Try

1 个答案:

答案 0 :(得分:1)

错误信息非常清楚。您正在尝试将SqlParameter数组传递给Add方法:

cmd.Parameters.Add(sqlParam)

也许您打算使用AddRange?

cmd.Parameters.AddRange(sqlParam)