简体   繁体   English

如何使用代码在VB中搜索access数据库的多个字段

[英]How to search multiple fields of an access database in VB using code

I have created a program which displays data from different tables in an access database(without the use of wizards to connect to the database. ie all connection have been hard coded), I have created a simple search which searches through one field in one table.我创建了一个程序,它在访问数据库中显示来自不同表的数据(不使用向导连接到数据库。即所有连接都已硬编码),我创建了一个简单的搜索,它搜索一个表中的一个字段. However I would like to make a function which allows me to search through all of the fields in a table.但是我想创建一个函数,它允许我搜索表中的所有字段。 Below is the simple search function I have created.下面是我创建的简单搜索功能。

     Public Sub Search()         
    con.Open()
    Dim dt As New DataTable("Table1")
    Dim rs As New OleDb.OleDbDataAdapter("Select * from Table1 where FirstName = '" & txtTabel1.Text & "'", con)

    rs.Fill(dt)
    dgvTabel2.DataSource = dt
    dgvTabel2.Refresh()

    rs.Dispose()
    con.Close()  
     end sub

The new function should look something like this新功能应该是这样的

   Public Sub SearchHard(TableName As String)
    con.Open()
    Dim dt As New DataTable("TableName")
    Dim rs As New OleDb.OleDbDataAdapter(("Select * from " & TableName & " where FirstName = '" & txtTabel1.Text & "'") Or ("Select * from " & TableName & " where LastName = '" & txtTabel1.Text & "')"), con)
    'SELECT * FROM MyTable WHERE FirstName LIKE '% txtTable1.text %' OR LastName LIKE '%txtTable1.tetx%'

    rs.Fill(dt)
    dgvTabel1.DataSource = dt
    dgvTabel1.Refresh()

    rs.Dispose()
    con.Close()
End Sub

It would also be a big plus if the function could accept a parameter to select a different table to reduce overusing the code.如果该函数可以接受一个参数来选择不同的表以减少过度使用代码,那也将是一个很大的优势。 Any and all help would be much appreciated任何和所有的帮助将不胜感激

Working code to with searches.用于搜索的工作代码。

    con.Open()
    Dim dt As New DataTable("Table1")
    Dim rs As New OleDb.OleDbDataAdapter("SELECT * FROM  Table1  WHERE (FirstName = '" & txtTabel1.Text & "') or (LastName = '" & txtTabel1.Text & "')", con)
    rs.Fill(dt)
    dgvTabel1.DataSource = dt
    dgvTabel1.Refresh()

    rs.Dispose()
    con.Close()

You have some wrong quotes in there:你在那里有一些错误的报价:

"Select * from Table where Field1 = '" & txtTabel1.Text & "'" or Field2 = '" & txtTabel1.Text"'"

should be应该

"Select * from Table where Field1 = '" & txtTabel1.Text & "' or Field2 = '" & txtTabel1.Text & "'"

Now of course you could use your parameter TableName like this现在你当然可以像这样使用你的参数TableName

"Select * from " & TableName & " where Field1 = '" & txtTabel1.Text & "'"

But different tables will have different field names, so that wouldn't really work.但是不同的表会有不同的字段名称,所以这不会真正起作用。 In the end you'd have to add lots of parameters and sort of re-invent SQL.最后,您必须添加大量参数并重新发明 SQL。

A better solution would be to pass the full SQL SELECT string to your function.更好的解决方案是将完整的 SQL SELECT 字符串传递给您的函数。

On top of what was already mentioned here by @andre451, you could use union if you want to search multiple tables除了@andre451 在这里已经提到的内容之外,如果要搜索多个表,可以使用union

SELECT field_1 , field_2
FROM table_1
WHERE field_3 = 'value' or field_4 = 'value'
UNION [ALL]
SELECT field_100 , field_200
FROM table_2
WHERE field_300 = 'value' or field_400 = 'value'

As long as your field_1 and field_100 match data types and count of fields matches只要您的field_1field_100匹配数据类型并且字段数匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM