In a typical multiple-tier implementation, the steps for creating and refreshing a DataSet, and in turn, updating the original data are to:
-
Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.
-
Change the data in individual DataTable objects by adding, updating, or deleting DataRow objects.
-
Invoke the GetChanges method to create a second DataSet that features only the changes to the data.
-
Call the Update method of the DataAdapter, passing the second DataSet as an argument.
-
Invoke the Merge method to merge the changes from the second DataSet into the first.
-
Invoke the AcceptChanges on the DataSet. Alternatively, invoke RejectChanges to cancel the changes.
DataRelation
ds.Tables.Add(table);
ds.Tables.Add(detailsTableSaved);
DataColumn[] pColumns = new DataColumn[] { table.Columns["DEPT_CODE"], table.Columns["CHECK_DATE"], table.Columns["INDEX_CODE"] };
DataColumn[] cColumns = new DataColumn[] { detailsTableSaved.Columns["DEPT_CODE"], detailsTableSaved.Columns["CHECK_DATE"], detailsTableSaved.Columns["INDEX_CODE"] };
DataRelation tDataRelation = new DataRelation("relation_Category_Product", pColumns, cColumns);
ds.Relations.Add(tDataRelation);
Master and Detail Relationship
// Add data from the Customers table to the DataSet.
SqlDataAdapter masterDataAdapter = new
SqlDataAdapter("select * from Customers", connection);
masterDataAdapter.Fill(data, "Customers");
// Add data from the Orders table to the DataSet.
SqlDataAdapter detailsDataAdapter = new
SqlDataAdapter("select * from Orders", connection);
detailsDataAdapter.Fill(data, "Orders");
// Establish a relationship between the two tables.
DataRelation relation = new DataRelation("CustomersOrders",
data.Tables["Customers"].Columns["CustomerID"],
data.Tables["Orders"].Columns["CustomerID"]);
data.Relations.Add(relation);
// Bind the master data connector to the Customers table.
masterBindingSource.DataSource = data;
masterBindingSource.DataMember = "Customers";
// Bind the details data connector to the master data connector,
// using the DataRelation name to filter the information in the
// details table based on the current row in the master table.
detailsBindingSource.DataSource = masterBindingSource;
detailsBindingSource.DataMember = "CustomersOrders";
Real Example:
private void btnSearch_Click(object sender, EventArgs e)
{
string a = "03-8月-2014";//dtpStart.Value.ToString("yyyy/MM/dd hh:mm:ss");
string b = "05-8月-2014";//dtpEnd.Value.ToString("yyyy-MM-dd hh:mm:ss");
gDtNursingIndexMasterResult = getTableData.getTableData("select * from NURSING_INDEX_CHECK_MASTER where CHECK_DATE Between '" + a + "' and '" + b + "'");
gDtNursingIndexMasterResult.Tables[0].TableName = "NURSING_INDEX_CHECK_MASTER";
gDtNursingIndexDetailResult = getTableData.getTableData("select * from NURSING_INDEX_CHECK_DETAIL where CHECK_DATE Between '" + a + "' and '" + b + "'");
gDtNursingIndexDetailResult.Tables[0].TableName = "NURSING_INDEX_CHECK_DETAIL";
gDtNursingIndexMasterResult.Merge(gDtNursingIndexDetailResult);
DataTable tblMaster = gDtNursingIndexMasterResult.Tables[0].Copy();
DataTable tblDetail = gDtNursingIndexDetailResult.Tables[0].Copy();
gDsMasterDetail.Tables.Add(tblMaster);
gDsMasterDetail.Tables.Add(tblDetail);
DataColumn[] pColumns = new DataColumn[] { tblMaster.Columns["DEPT_CODE"], tblMaster.Columns["CHECK_DATE"], tblMaster.Columns["INDEX_CODE"] };
DataColumn[] cColumns = new DataColumn[] { tblDetail.Columns["DEPT_CODE"], tblDetail.Columns["CHECK_DATE"], tblDetail.Columns["INDEX_CODE"] };
DataRelation tDataRelation = new DataRelation("MasterDetail", pColumns, cColumns);
gDsMasterDetail.Relations.Add(tDataRelation);
gBsMaster.DataSource = gDsMasterDetail;
gBsMaster.DataMember = "NURSING_INDEX_CHECK_MASTER";
gBsDetail.DataSource = gBsMaster;
gBsDetail.DataMember = "MasterDetail";
bnbSearchResult.BindingSource = gBsMaster;
cmbPatBedNo.DataBindings.Add("Text", gBsMaster, "BED_NO", true);
}
Generating Typed DataSets Using xsd.exe
File.WriteAllText(file\employees.xsd, DataSet2.GetXmlSchema());
c:\> xsd.exe employees.xsd /d /l:cs /n:Employees.Data
c:\> csc /target:library Employees.cs
Write and Read DataSet
file.WriteAllText(file "employees.xml", employeesTable.GetXml();