关于DataSet读了表后的操作问题
大家好:
我现在数据库里有两张表,两张表是带外键关联的.
我现在把这2个表都读到DataSet里去了,分别就是table[0]和table[1]
我现在想把这2张表的关联查询找出来,但不想通过数据库,可不可以通过dataset来实现,怎么实现呢?谢谢
------解决方案--------------------给个Demo
using System;
using System.Data;
using System.Data.SqlClient;
class DataRelationDemo
{
public static void Main()
{
SqlConnection cn = new SqlConnection(
@ "Data Source=(local);uid=sa;password=sa;Initial Catalog=northwind ");
SqlDataAdapter da = new SqlDataAdapter(
"SELECT CustomerID, CompanyName FROM Customers ", cn);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds= new DataSet();
SqlDataAdapter custAdapter = new SqlDataAdapter(
"SELECT * FROM Customers ", cn);
SqlDataAdapter orderAdapter = new SqlDataAdapter(
"SELECT * FROM Orders ", cn);
custAdapter.Fill(ds, "Customers ");
orderAdapter.Fill(ds, "Orders ");
DataRelation custOrderRel = ds.Relations.Add( "CustOrders ",
ds.Tables[ "Customers "].Columns[ "CustomerID "],
ds.Tables[ "Orders "].Columns[ "CustomerID "]);
foreach (DataRow custRow in ds.Tables[ "Customers "].Rows)
{
Console.WriteLine( "Customer ID: " + custRow[ "CustomerID "] +
" Name: " + custRow[ "CompanyName "]);
foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel))
{
Console.WriteLine( " Order ID: " + orderRow[ "OrderID "]);
}
}
}
}
------解决方案--------------------晕,竟然要分两次贴