使用 UNION ALL SQL 查询填充类型数据集
我这里有一个奇怪的情况,我希望有人可以帮助我。使用非类型化数据集时我没有遇到同样的问题。
好的。我有一个类型化数据集 MyDS 和一个类型化数据表 MyTable(TableID、标题、消息)。
该表使用 UNION ALL 填充了两个表的结果。
Select
TableAID,
TableATitle,
Message
FROM TableA
UNION ALL
Select
TableBID,
TableBTitle,
Message
FROM TableB
Dim cmd As SqlCommand = New SqlCommand("SQL", conn)
Dim da AS SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(MyDS.MyTable)
该查询在 SQL Server 上运行良好,但当我单步执行代码时,我发现不正确的 TableAID 或 TableAID。
表 A 有 1 行,表 B 有 15 行。所以我的数据集返回 16 行(所以基本上这是一个主从情况)。除了 TableAID 和 TableBID 之外,所有列都返回正确的相应数据。
TableAID 的值为 100,TableBID 的值为 10, 11, 12, 13, .........24,但是当我单步执行时,
MyDS.MyTable(i).TableID
remains 100 throughout the 16 records, instead of 100, 10, 11, 12, 13, .........24. The Title, Message data is correct. TableAID and TableBID are my primary keys.i = 0 到 15
这就是类型化数据集处理 UNION 的方式吗?我不想创建两个类型化数据表并为每个数据表创建单独的 SQL 并创建关系。
I have a strange situation here and I hope someone can help me. I don't have the same problem when using an untyped dataset.
OK. I have a typed dataset MyDS with a typed datatable MyTable (TableID, Title, Message).
The table gets filled with results from two tables, using a UNION ALL
Select
TableAID,
TableATitle,
Message
FROM TableA
UNION ALL
Select
TableBID,
TableBTitle,
Message
FROM TableB
Dim cmd As SqlCommand = New SqlCommand("SQL", conn)
Dim da AS SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(MyDS.MyTable)
The query runs well on SQL Server, but when I step through the code, I find incorrect TableAID or TableAID.
TableA has 1 row and TableB has 15 rows. So my dataset returns 16 rows (so basically it's a master-detail situation). All the columns return correct respective data, except the TableAID and TableBID.
TableAID has value = 100 and TableBID has 10, 11, 12, 13, .........24, but when I step through, the
MyDS.MyTable(i).TableID
remains 100 throughout the 16 records, instead of 100, 10, 11, 12, 13, .........24. The Title, Message data is correct. TableAID and TableBID are my primary keys.
i = 0 to 15
Is this how typed datasets handles UNION? I didn't want to create two typed datatables and create separate SQL for each and create a relation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法修复它。它根本不是数据集。我实际上使用的是从视图中进行选择的存储过程,并且我有 2 个几乎相同的视图(view1 和 view2)。所以上面的sql来自view2,但存储过程使用view1。我使用视图是因为语句比那复杂。所以我的动机是,如果我需要在存储过程中使用一个复杂的 SELECT sql,请将其放在视图中并使用一个简单的过程。
代码
不是
我几乎想用非类型化数据集替换我的类型化数据集,因为我已经没有时间了。截止日期。哈哈
感谢您让我认为这不是数据集,因为您无法弄清楚可能是什么问题。哈哈
I managed to fix it. It wasn't the dataset at all. I was actually using a stored proc that was selecting from a view, and I had 2 views (view1 and view2) that were almost the same. So the above sql was from view2, but the stored proc was using view1. I used views because the statement was complicated than that. So my moto is, if I have a complicated SELECT sql that I need to use in my stored proc, put it on view and have a simple proc to work with.
The code is
Not
I almost wanted to replace my typed dataset with untyped one, because I was running out of time. Deadlines. LOL
Thank you for making me think that it was not the dataset since you could not figure out what could be the problem. LOL