note :所有列数据都存储为nvarchar
我的搜索功能一直给我错误,我不明白。基本上,我有一个comboBox,用户可以选择将搜索过滤到datagridview中的特定列,然后他们可以在文本框中键入搜索并单击按钮进行搜索。
我得到的错误类型: 语法错误:'Native'运算符后缺少操作数。 //我输入了搜索文本框“Bold Native”并使用了列过滤器“Films” 无法在System.String和System.Int32上执行“Like”操作。 //我输入了搜索文本框“1979”并使用了列过滤器“Year”
非常简单的代码,只是数据库每列的一个很大的if / elseif语句(原来是破折号):
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
--If ComboBox1.Text = " No Filter " Then
----Me.FilmsBindingSource.Filter = Me.FilmsBindingSource.Filter = "Films LIKE " + TbFilter.Text + "% OR Actors LIKE " + TbFilter.Text + "% OR Directors LIKE " + TbFilter.Text + "% OR Writers LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Films" Then
----Me.FilmsBindingSource.Filter = "Films LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Year" Then
----Me.FilmsBindingSource.Filter = "Year LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Duration" Then
----Me.FilmsBindingSource.Filter = "Duration LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Actors" Then
----Me.FilmsBindingSource.Filter = "Actors LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Directors" Then
----Me.FilmsBindingSource.Filter = "Directors LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Writers" Then
----Me.FilmsBindingSource.Filter = "Writers LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Own It?" Then
----Me.FilmsBindingSource.Filter = "Own It? LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Seen it It?" Then
----Me.FilmsBindingSource.Filter = "Seen It? LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Rating" Then
----Me.FilmsBindingSource.Filter = "Rating LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Top 10" Then
----Me.FilmsBindingSource.Filter = "Top 10 LIKE " + TbFilter.Text
--ElseIf ComboBox1.Text = "Misc" Then
----Me.FilmsBindingSource.Filter = "Misc LIKE " + TbFilter.Text
--End If
End Sub
我正在尝试找出一种更好的过滤/搜索方法,但我还没有想出任何解决方案。有什么想法吗?
答案 0 :(得分:0)
尝试使用类似的东西。
如果用户没有选择任何项目,它将按ID搜索。否则,它将按所选字段(条件)进行搜索。
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button3.Click
Dim cmd As New SqlCommand
Dim criteria, value As String
value = InputBox("Please enter value: ")
If ComboBox1.SelectedIndex = 0 Then
cmd= New SqlCommand("Select * from DATA Where ID=@id", Connection)
cmd.Parameters.AddWithValue("@id", value)
Else
criteria = ComboBox1.SelectedItem.ToString
cmd = New SqlCommand("Select * from DATA where " & criteria _
& " like '%" & value & "%' ", Connection)
End If
dtTable.Rows.Clear()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dtTable)
Me.DataGridView1.DataSource = dtTable
End Sub