如何在一个gridview中查看两个表?

时间:2018-04-05 07:10:23

标签: mysql vb.net mysql-workbench

我有两个具有相同列的表。我想知道的是如何从数据库中查看两个表到一个数据网格视图?

这是我的代码

con = New MySqlConnection
con.ConnectionString = cons
ds = New Dataset
tables = ds.Tables

da = New MysqlDataAdapter("Select * from haspostingusername where User ='" & txtUser.Text & "'",con)
d1 = New MysqlDataAdapter("Select * from laspostingusername where User ='" & txtUser.Text & "'",con)
da.Fill(ds, "hapostingusername")
da1.Fill(ds, "lapostingusername")
Dim view As New DataView(tables(0))
DgvReport.DataSource = view
请帮助我。谢谢!

2 个答案:

答案 0 :(得分:0)

尝试使用union并一次性返回结果。 而不是

da = New MysqlDataAdapter("Select * from haspostingusername where User ='" & txtUser.Text & "'",con)
d1 = New MysqlDataAdapter("Select * from laspostingusername where User ='" & txtUser.Text & "'",con)

你可以尝试

foobar = New MysqlDataAdapter("Select * from haspostingusername where User ='" & txtUser.Text & " UNION Select * from laspostingusername where User ='" & txtUser.Text & "'",con)

此外,如果从用户输入txtUser,您应该将其包装在准备好的语句中以避免SQL注入。

答案 1 :(得分:0)

现在好了。非常感谢你! :)是我的代码:

da = New MysqlDataAdapter("Select * from haspostingusername where User ='" & txtUser.Text & "' UNION ALL Select * from laspostingusername where User ='" & txtUser.Text & "'",con)