来自单个查询的数据关系
我有以下一段 VB.NET 代码:
Dim conn As New MySql.Data.MySqlClient.MySqlConnection(ConnectionString)
conn.Open()
Dim sql = "SELECT * FROM users"
Dim com = New MySql.Data.MySqlClient.MySqlCommand(sql, conn)
Dim ds As New DataSet("dsUsers")
Dim da As New MySql.Data.MySqlClient.MySqlDataAdapter()
da.SelectCommand = com
da.TableMappings.Add("Table", "Users")
da.Fill(ds)
sql = "SELECT * FROM messages"
com = New MySql.Data.MySqlClient.MySqlCommand(sql, conn)
Dim da2 As New MySql.Data.MySqlClient.MySqlDataAdapter()
da2.SelectCommand = com
da2.TableMappings.Add("Table", "Messages")
da2.Fill(ds)
Dim dr As DataRelation
Dim col1 As DataColumn
Dim col2 As DataColumn
col1 = ds.Tables("Users").Columns("id")
col2 = ds.Tables("Messages").Columns("users_id")
dr = New DataRelation("UsersMessages", col1, col2)
ds.Relations.Add(dr)
基本上,我执行两个查询:第一个查询获取所有用户,第二个查询获取所有消息。 我在 DataSet 中定义两个表,并通过 DataRelation 链接它们,以便 Users 表是 Messages 表的父表。
如果我想从两个表中选择所有行,那么这是可行的,但如果我有一个包含 4 个嵌套表和条件查询的更复杂的结构怎么办?
SELECT t1.*, t2*, t3.*, t4.*
FROM table1 t1, table2 t2, table3 t3, table4 t4
WHERE t1.id = 3
AND t2.t1_id = t1.id
AND t3.t2_id = t2.id
AND t4.t3_id = t3.id
如何从这个单一查询创建一个包含四个表和三个数据关系的数据集?
谢谢
i have the following piece of VB.NET code:
Dim conn As New MySql.Data.MySqlClient.MySqlConnection(ConnectionString)
conn.Open()
Dim sql = "SELECT * FROM users"
Dim com = New MySql.Data.MySqlClient.MySqlCommand(sql, conn)
Dim ds As New DataSet("dsUsers")
Dim da As New MySql.Data.MySqlClient.MySqlDataAdapter()
da.SelectCommand = com
da.TableMappings.Add("Table", "Users")
da.Fill(ds)
sql = "SELECT * FROM messages"
com = New MySql.Data.MySqlClient.MySqlCommand(sql, conn)
Dim da2 As New MySql.Data.MySqlClient.MySqlDataAdapter()
da2.SelectCommand = com
da2.TableMappings.Add("Table", "Messages")
da2.Fill(ds)
Dim dr As DataRelation
Dim col1 As DataColumn
Dim col2 As DataColumn
col1 = ds.Tables("Users").Columns("id")
col2 = ds.Tables("Messages").Columns("users_id")
dr = New DataRelation("UsersMessages", col1, col2)
ds.Relations.Add(dr)
Basically, i execute two queries: the first one fetches all the users, the second all the messages. I define two tables within a DataSet and link them through a DataRelation so that the Users table is a parent of Messages table.
This works if I want to select all the rows from the two tables, but what if I have a more complex structure with 4 nested tables and conditional queries?
SELECT t1.*, t2*, t3.*, t4.*
FROM table1 t1, table2 t2, table3 t3, table4 t4
WHERE t1.id = 3
AND t2.t1_id = t1.id
AND t3.t2_id = t2.id
AND t4.t3_id = t3.id
How can I create a DataSet with four tables and the three DataRelations from this single query?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
做到这一点的唯一方法是编写执行此操作的代码,即在数据集中创建表,设置表之间的关系并使用结果中的某些字段填充表。
没有自动执行此操作的方法,因为结果中没有有关每个字段来自哪个表的信息,甚至没有查询哪些表来获取结果的信息。
The only way to do that would be to write code that does it, i.e. create tables in the data set, set up the relations between the tables and populate the tables with certain fields from the result.
There is no automatic way of doing this, as there is no information in the result about which table each field comes from, or even which tables were queried to get the result.