简体   繁体   English

如何在vb.net中使用select查询在数据库中的所有表中搜索数据?

[英]How to search data in all tables in database using select query in vb.net?

How to search data in all tables in database using select query in vb.net?如何在vb.net中使用select查询在数据库中的所有表中搜索数据? Here is my code:这是我的代码:

Try
    mysqlconn.Open()

    Dim query As String
    query = "select * from  where (Item_Description LIKE '%" & TextBox11.Text & "%' or Vendor LIKE '%" & TextBox11.Text & "%' OR S_N LIKE '%" & TextBox11.Text & "%' or Tag_num LIKE '%" & TextBox11.Text & "%')"
    command = New MySqlCommand(query, mysqlconn)
    sda.SelectCommand = command
    sda.Fill(dbdataset)
    bsource.DataSource = dbdataset
    DataGridView1.DataSource = bsource
    sda.Update(dbdataset)
    mysqlconn.Close()

Catch ex As Exception

Finally
    mysqlconn.Dispose()
End Try

Sorry Ana I don't think you can leave out the table name in the from clause.抱歉,安娜,我认为您不能在 from 子句中省略表名。 If you really want to search 30 different tables (that all have the same columns...?) then you'd likely have to iterate over each one of them separately and join the information yourself如果你真的想搜索 30 个不同的表(它们都有相同的列......?)那么你可能必须分别迭代它们中的每一个并自己加入信息

You could do a for each (Table Name) loop to get the data from each table.您可以执行 for each (Table Name) 循环以从每个表中获取数据。 The adapter adds all the new information into the datatable so that you have just 1 datatable in the end with results from all 30 tables.适配器将所有新信息添加到数据表中,这样您最终只有 1 个数据表,其中包含来自所有 30 个表的结果。

Dim query As String
Dim dt as new Datatable
For each tablename in (tablenamelist) 
    query = "select * from " & tablename & " where (Item_Description LIKE '%" & TextBox11.Text & "%' or Vendor LIKE '%" & TextBox11.Text & "%' OR S_N LIKE '%" & TextBox11.Text & "%' or Tag_num LIKE '%" & TextBox11.Text & "%')"
    command = New MySqlCommand(query, mysqlconn)
    sda.SelectCommand = command
    sda.Fill(dt)
next

where tablenamelist is a list of all your datatable names.其中 tablenamelist 是所有数据表名称的列表。 This will get the job done, but there are many improvements to be made here.这将完成工作,但这里有许多改进。

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

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