DataSet key points

本文详细阐述了在典型多层实现中创建和刷新数据集的过程,包括填充数据集、更新数据、创建变更数据集、更新适配器、合并变化以及接受或拒绝更改。通过示例展示了如何使用ADO.NET管理数据关系和建立主从关系。实例如何根据日期筛选数据并合并数据集,最后介绍了使用xsd.exe生成TypedDataSet和读写DataSet的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In a typical multiple-tier implementation, the steps for creating and refreshing a DataSet, and in turn, updating the original data are to:

  1. Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.

  2. Change the data in individual DataTable objects by adding, updating, or deleting DataRow objects.

  3. Invoke the GetChanges method to create a second DataSet that features only the changes to the data.

  4. Call the Update method of the DataAdapter, passing the second DataSet as an argument.

  5. Invoke the Merge method to merge the changes from the second DataSet into the first.

  6. 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();

 

转载于:https://www.cnblogs.com/kevinygq/p/3886864.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值