我一直在尝试将SQLcmd参数用于我的代码:
Dim Conn As SqlConnection = New SqlConnection(MSSQLstring)
Conn.Open()
Dim strUserCode = CType(Session("usrCd"), String)
Dim SQLstring As String
If strUserCode = "ADMIN" Then
SQLstring = "SELECT COUNT(AskAOID) AS credits, CatCode AS category, ProductCode " _
& "FROM dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN @dateFrom; AND @dateTo;) " _
& "AND (Status='ACTIVE' OR Status='CLOSED') " _
& "AND (FirstResponse IS NOT NULL) AND (CatCode IS NOT NULL) AND (ProductCode IS NOT NULL) " _
& "GROUP BY ProductCode, CatCode " _
& "ORDER BY CatCode"
Else
SQLstring = "SELECT COUNT(AskAOID) AS credits, CatCode AS category, ProductCode " _
& "FROM dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN @dateFrom; AND @dateTo;) " _
& "AND (Status='ACTIVE' OR Status='CLOSED') " _
& "AND (Deferred = '" & strUserCode & "') " _
& "AND (FirstResponse IS NOT NULL) AND (CatCode IS NOT NULL) AND (ProductCode IS NOT NULL) " _
& "GROUP BY ProductCode, CatCode " _
& "ORDER BY CatCode"
End If
Dim SQLcmd As SqlCommand = New SqlCommand(SQLstring, Conn)
SQLcmd.Parameters.AddWithValue("@dateFrom", dpFrom.SelectedDate.ToString)
SQLcmd.Parameters.AddWithValue("@dateTo", dpTo.SelectedDate.ToString)
Dim DA As New SqlDataAdapter(SQLstring, Conn)
Dim DS As New DataSet
DA.Fill(DS)
Conn.Close()
dpFrom和dpTo是日期选择器,我试图消除我的SQL声明中的连接。不幸的是,从上面的代码中,我收到此错误消息:
必须声明标量变量“@dateFrom”。
到目前为止我还没有使用SQLcmd参数别名,所以我想知道我做错了什么:(
全部谢谢:)
更新
我也尝试了以下代码更改,但我没有运气:(
Dim Conn As SqlConnection = New SqlConnection(MSSQLstring)
Conn.Open()
Dim strUserCode = CType(Session("usrCd"), String)
Dim SQLstring As String
If strUserCode = "ADMIN" Then
SQLstring = "SELECT COUNT(AskAOID) AS credits, CatCode AS category, ProductCode " _
& "FROM dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN @dateFrom AND @dateTo) " _
& "AND (Status='ACTIVE' OR Status='CLOSED') " _
& "AND (FirstResponse IS NOT NULL) AND (CatCode IS NOT NULL) AND (ProductCode IS NOT NULL) " _
& "GROUP BY ProductCode, CatCode " _
& "ORDER BY CatCode"
Else
SQLstring = "SELECT COUNT(AskAOID) AS credits, CatCode AS category, ProductCode " _
& "FROM dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN @dateFrom AND @dateTo) " _
& "AND (Status='ACTIVE' OR Status='CLOSED') " _
& "AND (Deferred = '" & strUserCode & "') " _
& "AND (FirstResponse IS NOT NULL) AND (CatCode IS NOT NULL) AND (ProductCode IS NOT NULL) " _
& "GROUP BY ProductCode, CatCode " _
& "ORDER BY CatCode"
End If
Dim SQLcmd As SqlCommand = New SqlCommand(SQLstring, Conn)
SQLcmd.Parameters.AddWithValue("@dateFrom", dpFrom.SelectedDate)
SQLcmd.Parameters.AddWithValue("@dateTo", dpTo.SelectedDate)
Dim DA As New SqlDataAdapter(SQLstring, Conn)
Dim DS As New DataSet
DA.Fill(DS)
Conn.Close()