021. asp.net两个DataSet数据集的合并

    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet dsSource = new DataSet();      //创建源数据集
        DataTable dt = CreateDataTable();      //创建本地表
        dsSource.Tables.Add(dt);               //将创建的本地表添加到源数据集中

        DataSet copyDataSet1 = dsSource.Copy();//复制整个数据集(包含数据)
        //仅复制源数据集中表数据被更改的部分
        DataSet copyDataSet2 = dsSource.GetChanges();
        //仅复制源数据集中表数据行状态为添加状态的部分
        DataSet copyDataSet3 = dsSource.GetChanges(DataRowState.Added);
        //仅复制源数据集的架构
        DataSet copyDataSet4 = dsSource.Clone();
        //取得源数据集中指定表的指定数据
        DataRow[] copyRows = dsSource.Tables[0].Select("productCode = '0001' ");
        //将取得的数据数组导入到仅复制架构的数据集表中
        DataTable tbSource = copyDataSet4.Tables[0];
        foreach (DataRow copyRow in copyRows)
        {
            tbSource.ImportRow(copyRow);
        }
        GridView1.DataSource = tbSource;
        GridView1.DataBind();
    }

    private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("productCode", typeof(string));
        dt.Columns.Add("saleDate", typeof(DateTime));
        dt.Columns.Add("saleAmount", typeof(double));
        DataRow dr = dt.NewRow();
        dr["productCode"] = "0001";
        dr["saleDate"] = Convert.ToDateTime("2009-2-1");
        dr["saleAmount"] = 1000;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["productCode"] = "0001";
        dr["saleDate"] = Convert.ToDateTime("2009-1-1");
        dr["saleAmount"] = 2000;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["productCode"] = "0002";
        dr["saleDate"] = Convert.ToDateTime("2009-1-1");
        dr["saleAmount"] = 3000;
        dt.Rows.Add(dr);
        return dt;
    }

 

posted on 2016-12-08 13:41  印子  阅读(1348)  评论(0编辑  收藏  举报

导航