我有多个SQL表,我用它来填充整个应用程序中的数据网格视图和文本框。我想在应用程序的主页面上创建一个全局搜索功能,以便从搜索框中搜索所有表格并将其显示在文本框中。我不知道从哪里开始。文本框只是查看详细信息,无需保存,编辑或删除。我目前有一个搜索框和一个搜索按钮,然后是一个名为results
的文本框。
这就是我所拥有的......
Private Sub GlobalSearchbtn_Click(sender As Object, e As EventArgs) Handles GlobalSearchbtn.Click
Try
conn.Open()
Dim sql1 As String
sql1 = "SELECT * from sys.objects where type like '" & txtGlobalsearch.Text & "%'"
cmd = New SqlCommand(sql1, conn)
reader = cmd.ExecuteReader
While reader.Read
txtresults.Text = reader(1).Tostring
End While
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
答案 0 :(得分:-1)
您必须遍历数据库中存在的所有表。然后,对于每个表,您必须检索所有字段,然后选择varchar / nvarchar。最后,您必须构建一个动态请求来查询此表中此字段的结果。
我不能完全告诉你你要做的事情,但我可以给你写一个algorythm的例子
var tableQuery = "SELECT * FROM sys.objects WHERE [FieldForTheTypeOfTheObject] = 'table'"
//execute and loop on each table
foreach table in tableQueryResult
var fieldQuery = "SELECT * FROM [sys.objects or maybe another table] WHERE [FieldOfTheTypeOfTheObject] = 'field'"
foreach field in fieldQueryResult
if(field.Type == 'varchar' || field.Type == 'nvarchar') then
var finalQuery = "SELECT * FROM " & table.Name & " WHERE " & field.Name & " like '%" & txtGlobalsearch.Text.Replace("'", "''") & "&'"
//Execute query and display data
endif
end
end