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);
}
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();
}
}
}