我正在尝试根据查询字符串动态更改SQLDataAdapter中的查询。我尝试将其作为参数传递,但这不起作用。还有其他建议吗?
现在我有:
Dim conn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(WebConfigurationManager.ConnectionStrings("MyConnection").ToString())
Dim ad As New SqlDataAdapter
Dim selectSQL As String = "SELECT BookTitle, Publisher, Location, CallNumber, [Extra_Description] AS Description, [ISBN] FROM [New_Book_Table] WHERE [Export_to_list]='True' AND [@Category]='True'"
Dim selectCMD As SqlCommand = New SqlCommand(selectSQL, conn)
ad.SelectCommand = selectCMD
selectCMD.Parameters.Add("@Category", SqlDbType.NVarChar, 30).Value = getCategoryString()
Dim ds As New DataSet
ad.Fill(ds)
ListView1.DataSource = ds
ListView1.DataBind()
但是得到错误:System.Data.SqlClient.SqlException: Invalid column name '@Category'.
我想因为这不是正确的方法。我怎样才能做到这一点?我可以通过查询字符串获取列名。我想将该列名称传递给查询,并将其识别为列。
答案 0 :(得分:0)
好吧我想出来了,这比尝试动态添加参数更简单,因为最初的SelectSQL只是一个字符串,我用以下函数动态构造了该字符串:
Protected Function getSQLQuery() As String
Dim baseQuery As String = "SELECT BookTitle, Publisher, Location, CallNumber, [Extra_Description] AS Description, [ISBN] FROM [New_Book_Table] WHERE [Export_to_list]='True'"
Dim category As String = getCategoryString()
Return baseQuery & "AND [" & category & "]='True'"
End Function