如何从相关表中返回尽可能少的行
我是 ADO.net 新手,遇到这个问题:
假设我在 SQL Server 2005 数据库中有这两个表,其中包含以下列:
[Orders]
- OrderID
- OrderDate
- ShopID
- TotalAmount
- TotalTaxAmount
- 等...
[OrdersDetails]
- OrderID
- ShopID
- ItemID
- Quantity
- Amount
- TaxAmount
- 等
我已经启动了一个 WinForms 应用程序来开始。 在此表单中,用户可以选择商店列表并选择日期范围以查看该商店的所有订单。
我从 Visual Studio 添加了一个数据源,选择 Orders 和 OrdersDetails 表,并将 Orders 和相关 OrdersDetails 表拖放到 DataGridViews 表单中。
当我从 Orders DataGridView 中选择一行时,我确实在第二个 DataGridView 中看到了我想要的相应订单详细信息。我在这个数据库中建立了关系,ADO.net 捕获了它们并将它们反映在数据集中。
然后,我向键入的数据集添加了一个方法,以按 OrderDate 和 ShopID 列获取数据。 由于 OrdersDetails 表没有 OrderDate 列,因此我只能按 ShopID 对其进行过滤。
问题是从 OrdersDetails 获取记录非常耗时,因为它将检索到 OrdersDetails 数据表中超出所需的行数。 问题是我只能按 ShopID 过滤 OrderDetails 表中的行,这会从数据库返回太多记录。
显然,ADO.net 能够通过使用 OrderID 关系在客户端适当地过滤它们,但仅从 OrdersDetails 中检索我实际需要的行会更有意义。
我已经修改了从第二个表获取数据的查询,以使用联接添加 OrderDate,因此当我从数据库检索数据时,我可以按日期进行过滤...但是,当我尝试更新我的更改时,它会导致问题到这个外国专栏...
我相信一定有一个简单的方法来解决这个问题,不是吗?
预先非常感谢。
I am new to ADO.net and have this problem:
Let's assume I have these two tables in a SQL Server 2005 database, with these columns:
[Orders]
- OrderID
- OrderDate
- ShopID
- TotalAmount
- TotalTaxAmount
- etc...
[OrdersDetails]
- OrderID
- ShopID
- ItemID
- Quantity
- Amount
- TaxAmount
- etc
I have started a WinForms application to get myself started.
In this form, the user can select a list of Shops and select a date range to see all orders from this shop.
I have added a data source from Visual Studio, select both Orders and OrdersDetails table and dragged and dropped the Orders and related OrdersDetails tables into the form as DataGridViews.
When I select a row from the Orders DataGridView, I indeed see the corresponding Orders Details in the second DataGridView as I wanted. I had relationships inside this database and ADO.net caught them up and reflected them in the dataset.
I have then added a method to my typed dataset to get data by the OrderDate, and ShopID column.
As the OrdersDetails table does not have an OrderDate column, I could only filter it by ShopID.
The issue is that it is time consuming to get the records from the OrdersDetails as it will retrieve more rows than needed into the DataTable for the OrdersDetails.
The problem is that I can only filter the rows from the OrderDetails table by ShopID, which returns way too many records from the database.
Obviously, ADO.net is able to filter them appropriately on the client-side by using the OrderID relationship but it would make much more sense to retrieve only the rows from the OrdersDetails that I actually need.
I have modified my queries getting the data from the second table to add the OrderDate using a join, so I can filter by date when I retrieve the data from the database... however, it causes problems when I try to update my changes due to this foreign column...
I believe there must be an easy way around this, isn't there?
Thanks a lot in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想做这样的事情
You want to do something like this
@David,最好编写表达您的意图并反映性能更高的算法的代码,而不是依赖引擎的实现细节来执行优化。
@David it's probably better to write code which expresses your intent and reflects the more performant algorithm, rather than relying on implementation details of the engine to perform the optimization.