在C#的DataSet中可以进行多表查寻吗?

moznan 2003-06-28 05:47:24
在C#中,我已经对DataSet进行了数据填充,但是我想进一步对DayaSet中的表进行多表查寻,可以吗?如何做?
...全文
97 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
orcale 2003-07-02
  • 打赏
  • 举报
回复
private void CreateRelation() {
// Get the DataColumn objects from two DataTable objects in a DataSet.
DataColumn parentCol;
DataColumn childCol;
// Code to get the DataSet not shown here.
parentCol = DataSet1.Tables["Customers"].Columns["CustID"];
childCol = DataSet1.Tables["Orders"].Columns["CustID"];
// Create DataRelation.
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol);
// Add the relation to the DataSet.
DataSet1.Relations.Add(relCustOrder);
}
moznan 2003-07-02
  • 打赏
  • 举报
回复
哦,对不起,我可能没有说清楚,是这样:
我通过您的上述方法把数据库中的表A和表B填充到DataSet中的DA和DB中,然后在DA和DB中如何进行多表查寻,来实现select ....left join on ....语句的功能.
如果在SqlDataAdapter的SelectCommand中使用select..left join语句,我对DataSet中数据修改后,其结构是不能返回到数据库中.
OpenVMS 2003-07-01
  • 打赏
  • 举报
回复
查询:改一下

把metaDataDataGrid绑定到myDataSet.Tables集合,而不是DataTables其中之一,就可实现多表查询

ordersDataGrid.DataSource = myDataSet.Tables["Orders"];
metaDataDataGrid.DataSource = myDataSet.Tables;
Page.DataBind();
OpenVMS 2003-07-01
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public class DataSetExample : Page
{
//Map the Web Form server controls
protected DataGrid customersDataGrid, ordersDataGrid;
private void BindData()
{
//Create a new DataSet with the DataSetName value "Northwind"
DataSet myDataSet = new DataSet("Northwind");
//Create the T-SQL and ConnectionString values
String mySqlStmt ="SELECT TOP 10 CustomerID, CompanyName, " +
"ContactName, ContactTitle FROM Customers";
String myConString =
"server=localhost;database=Northwind;uid=sa;pwd=;";
//Construct a new SqlDataAdapter with the preceding values
SqlDataAdapter myDataAdapter =
new SqlDataAdapter(mySqlStmt, myConString);
//Invoke the Fill() method to create a
//new DataTable in the DataSet
myDataAdapter.Fill(myDataSet, "Customers");
//Change the DataAdapter's SelectCommand
mySqlStmt = "SELECT OrderID, CustomerID,OrderDate, " +
"RequiredDate, ShippedDate FROM ORDERS";
myDataAdapter.SelectCommand.CommandText = mySqlStmt;
//Invoke the Fill() method to create a
//new DataTable in the DataSet
myDataAdapter.Fill(myDataSet, "Orders");
customersDataGrid.DataSource = myDataSet.Tables["Customers"];
ordersDataGrid.DataSource = myDataSet.Tables["Orders"];
Page.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//Create a new DataSet by invoking
//the BindData() method
BindData();
}
}
}
moznan 2003-07-01
  • 打赏
  • 举报
回复
各位大侠,请指教啊!救救我!!
moznan 2003-06-28
  • 打赏
  • 举报
回复
To:tj_dns
如何做?能否写个样例代码看看?
愉快的登山者 2003-06-28
  • 打赏
  • 举报
回复
应该可以.

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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