高手们帮帮忙,一个较有研究意义的DataSet问题,大家帮帮忙!!!

aiyaya 2003-12-03 04:59:57
例如: 我有三张表A、B、C 。 A为主表,a为A表的主键。B、C表中也具有a 。
我想通过查询A表获得的满足要求的a,把相关的B表中的b和C表中的c用DataGrid
显示出来。显示如:

a b c

1、 X Y Z
2、 XX YY ZZ

(X、Y、Z和XX、YY、ZZ都是相关的)

请问能做到吗,怎么做?如果不能用DataSet做,那还有其他方法吗?

高手们帮帮忙!!! (分不够再加)
...全文
29 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinanewway 2003-12-05
  • 打赏
  • 举报
回复
还是在关系数据库中用SQL语句比较快吧
select A.a,B.b,C.c
from A
left outer join B on A.a=B.a
left outer join C on A.a=C.a
where 条件语句
momoguagua 2003-12-05
  • 打赏
  • 举报
回复
不用SQL的话,楼上的方法正解
philipsslg 2003-12-04
  • 打赏
  • 举报
回复
对 zjsen(脚踏实地) 说
如果你用向导生成sqlAdapter;
这句语句
strSQL="select * from table";
sqlDtApt = new SqlDataAdapter(strSQL,sqlConn);
是不是会覆盖
sqlAdapter的selectCommand

yellow888 2003-12-04
  • 打赏
  • 举报
回复
一个较愚蠢的问题: 增加筛选条件会不会查询速度更慢?
rottenapple 2003-12-04
  • 打赏
  • 举报
回复
如果是关系型数据库用sql语句写当然好了
如果是从其它文件得来的,例如xml文件,那么可以使用dataview
yaopaopao 2003-12-04
  • 打赏
  • 举报
回复
用Sql快,数据库内运行比在程序中快多了,所有有关数据库中表的操作都让数据库去执行
aiyaya 2003-12-04
  • 打赏
  • 举报
回复
由于数据量比较大,是直接用SQL查快呢,还是用DataRelation方式快???
分不够再加 一百分 ,高手们踊跃发言呀!!!
qiaoba 2003-12-04
  • 打赏
  • 举报
回复
标准答案:


DataRelation 的主要功能之一是允许您在 DataSet 中从一个 DataTable 导航至另一个 DataTable。它使您能够在给定相关 DataTable 中的单个 DataRow 的情况下检索一个 DataTable 中的所有相关 DataRow 对象。例如,当建立客户表和订单表之间的 DataRelation 后,可以使用 DataRow.GetChildRows 检索特定客户行的所有订单行。

以下代码示例创建 DataSet 中 Customers 表和 Orders 表之间的 DataRelation,并返回每个客户的所有订单。

[Visual Basic]
Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", _
custDS.Tables("Customers").Columns("CustomerID"), _
custDS.Tables("Orders").Columns("CustomerID"))

Dim custRow As DataRow
Dim orderRow As DataRow

For Each custRow in custDS.Tables("Customers").Rows
Console.WriteLine(custRow("CustomerID"))
For Each orderRow in custRow.GetChildRows(custOrderRel)
Console.WriteLine(orderRow("OrderID"))
Next
Next

[C#]
DataRelation custOrderRel = custDS.Relations.Add("CustOrders",
custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]);
foreach (DataRow custRow in custDS.Tables["Customers"].Rows)
{
Console.WriteLine(custRow["CustomerID"]);
foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel))
Console.WriteLine(orderRow["OrderID"]);
}

下一示例以上例为基础,将四个表关联在一起,并在导航这些关系。如上例所示,CustomerID 使 Customers 表与 Orders 表相关。对于 Customers 表中的每个客户,将确定 Orders 表中的所有子行,以返回特定客户的订单数以及他们的 OrderID 值。

该扩展示例还将返回 OrderDetails 表和 Products 表中的值。Orders 表使用 OrderID 与 OrderDetails 表相关,以确定在每一客户订单中订购的产品及数量。由于 OrderDetails 表只包含已订购产品的 ProductID,OrderDetails 将使用 ProductID 与 Products 相关,以返回 ProductName。在这一关系中,Products 表为父表,而 Order Details 表为子表。因此,当循环访问 OrderDetails 表时,将调用 GetParentRow 来检索相关的 ProductName 值。

请注意,当为 Customers 表和 Orders 表创建 DataRelation 时,没有为 createConstraints 标志指定任何值(默认为 true)。它假定 Orders 表中的所有行都具有一个存在于父 Customers 表中的 CustomerID 值。如果 CustomerID 存在于 Customers 表之外的 Orders 表中,则 ForeignKeyConstraint 将引发异常。

如果子列可能包含父列不包含的值,添加 DataRelation 时请将 createConstraints 标志设置为 false。在该示例中,对于 Orders 表和 OrderDetails 表之间的 DataRelation,createConstraints 标志将设置为 false。这样,应用程序就可以返回 OrderDetails 表中的所有记录并只返回 Orders 表中记录的子集,而不会生成运行时异常。该扩展示例生成以下格式的输出。

Customer ID: NORTS
Order ID: 10517
Order Date: 4/24/1997 12:00:00 AM
Product: Filo Mix
Quantity: 6
Product: Raclette Courdavault
Quantity: 4
Product: Outback Lager
Quantity: 6
Order ID: 11057
Order Date: 4/29/1998 12:00:00 AM
Product: Outback Lager
Quantity: 3

以下代码示例是一个扩展示例,在该示例中将返回 OrderDetails 表和 Products 表中的值,并只返回 Orders 表中记录的子集。

[Visual Basic]
Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", _
custDS.Tables("Customers").Columns("CustomerID"), _
custDS.Tables("Orders").Columns("CustomerID"))

Dim orderDetailRel As DataRelation = custDS.Relations.Add("OrderDetail", _
custDS.Tables("Orders").Columns("OrderID"), _
custDS.Tables("OrderDetails").Columns("OrderID"), false)

Dim orderProductRel As DataRelation = custDS.Relations.Add("OrderProducts", _
custDS.Tables("Products").Columns("ProductID"), _
custDS.Tables("OrderDetails").Columns("ProductID"))

Dim custRow, orderRow, detailRow As DataRow

For Each custRow In custDS.Tables("Customers").Rows
Console.WriteLine("Customer ID:" & custRow("CustomerID").ToString())

For Each orderRow In custRow.GetChildRows(custOrderRel)
Console.WriteLine(" Order ID: " & orderRow("OrderID").ToString())
Console.WriteLine(vbTab & "Order Date: " & orderRow("OrderDate").ToString())

For Each detailRow In orderRow.GetChildRows(orderDetailRel)
Console.WriteLine(vbTab & " Product: " & detailRow.GetParentRow(orderProductRel)("ProductName").ToString())
Console.WriteLine(vbTab & " Quantity: " & detailRow("Quantity").ToString())
Next
Next
Next
gordenfl 2003-12-04
  • 打赏
  • 举报
回复
ds.Table["c"].Select(bb.*,cc.* from A aa,B bb,C cc where cc.a = aa.a and bb.a = aa.a and aa.a = 你的条件)
火电 2003-12-04
  • 打赏
  • 举报
回复


select
bb.*,
cc.*
from
A aa,
B bb,
C cc
where cc.a = aa.a
and bb.a = aa.a
and aa.a = 你的条件

前提是b,c表的字段a是A表得外键
就是一条sql
aiyaya 2003-12-04
  • 打赏
  • 举报
回复
目前具体情况是这样的:

1 查询条件会变化,但它只是起过滤作用;

2 查询期间DB不变(查的是备份库),因DB数据量过大,
只会在晚上不工作时把运行库导入备份库更新;

3 因过滤条件存在一对多的情况,而且可以选择几个条件
复合查询,所以最好能直接在DataSet里过滤获得需要的数据。

SQL到DataTable后,应该也能过滤吧???
wkevin27 2003-12-04
  • 打赏
  • 举报
回复
是用sql语句,还是load到内存(DataSet)中,我觉得要看这么两点:
1,查询条件会不会变化,是不是一次查询,多次使用(这是DataSet的长项,也是微软设计它的出发点);
2,查询期间DB会不会改变,同步状况如何;
我觉得有时候单单考虑实现技术是不行的,还要结合用户需求。
simanh 2003-12-03
  • 打赏
  • 举报
回复
up
aiyaya 2003-12-03
  • 打赏
  • 举报
回复
如果用DataRelation怎么做啊,能实现吗?
rock1981 2003-12-03
  • 打赏
  • 举报
回复
看了一下b因你的几张表是关联的,所以最好的选择是把它们做的关系DataRelation建立起来
通过它们的级联关系很容易得到你想要的数据。
yellow888 2003-12-03
  • 打赏
  • 举报
回复
学习中ing...
tonyye1979 2003-12-03
  • 打赏
  • 举报
回复
挺热闹的嘛。来晚了!
支持用DataTable!!!
八爪鱼-杭州 2003-12-03
  • 打赏
  • 举报
回复
string strConn=""user id=sa;" +
"password=;" +
"initial catalog=northwind;" +
"data source=MyComputerName\\NetSDK;" +
"Connect Timeout=5";
sqlConn=new SqlConnection(strconn);
sqlConn.Open();
strSQL="select * from table";
sqlDtApt = new SqlDataAdapter(strSQL,sqlConn);
DateSetdtset = new DataSet();
sqlDtApt.Fill(dtset);
DataTable dt=dtset.Tables[0];
philipsslg 2003-12-03
  • 打赏
  • 举报
回复
写程序时,怎样用SQL语句生成DataTable,上面几个能不能写的具体点
伪装绅士 2003-12-03
  • 打赏
  • 举报
回复
我倒是主张用关系来做,DataRelation,这回比较好。
加载更多回复(11)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧