C#的数据集:DataSet对象

目录

一、合并DataSet内容

1.源码

2.生成效果

[二、 复制DataSet内容](#二、 复制DataSet内容)

1.源码

2.生成效果


DataSet对象就像存放于内存中的一个小型数据库。它可以包含数据表、数据列、数据行、视图、约束以及关系。通常,DataSet的数据来源于数据库或者XML,为了从数据库中获取数据,需要使用数据适配器(DataAdapter)从数据库中查询数据。

一、合并DataSet内容

可以使用DataSet的Merge()方法将DataSet、DataTable或DataRow数组的内容并入现有的DataSet中。

Merge()方法将指定的DataSet及其架构与当前的DataSet合并,在此过程中,将根据给定的参数保留或放弃在当前DataSet中的更改并处理不兼容的架构。 语法如下:

cs 复制代码
public void Merge
(
    DataSet dataSet,
    bool preserveChanges,
    MissingSchemaAction missingSchemaAction
)
☑ dataSet:其数据和架构将被合并到DataSet中。
☑ preserveChanges:要保留当前DataSet中的更改,则为true;否则为false。
☑ missingSchemaAction:MissingSchemaAction枚举值之一。

MissingSchemaAction枚举成员及说明

|--------------|------------------------------------------------------------------------|
| 枚举成员 | 说 明 |
| Add | 添加必需的列以完成架构 |
| AddWithKey | 添加必需的列和主键信息以完成架构,用户可以在每个DataTable上显式设置主键约束。这将确保对与现有记录匹配的传入记录进行更新,而不是追加 |
| Error | 如果缺少指定的列映射,则生成InvalidOperationException |
| Ignore | 忽略额外列 |

1.源码

cs 复制代码
//Form1.cs
//使用DataSet的Merge()方法将DataSet、DataTable或DataRow数组的内容并入现有的DataSet中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection conn;

        private void Form1_Load(object sender, EventArgs e)
        {
            //conn = new SqlConnection("Server=DESKTOP-QFENBNJ\\SQL_WEN;integrated security=SSPI;Initial Catalog=db_CSharp");
            conn = new SqlConnection("Server=DESKTOP-GFMO83R;integrated security=SSPI;Initial Catalog=db_CSharp");
            DataSet ds = new DataSet();                                             //创建数据集
            DataSet ds1 = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select * from tb_test", conn); //创建用于连接SQL的适配器对象
            sda.Fill(ds);                                                           //填充数据集
            SqlDataAdapter sda1 = new SqlDataAdapter("select * from tb_man", conn); 
            /*SqlCommandBuilder sbl = new SqlCommandBuilder(sda1);   */             //创建适配器sda1的SqlCommandBuilder对象,可删除
            sda1.Fill(ds1);
            ds1.Merge(ds, true, MissingSchemaAction.Add/*WithKey*/);                //数据集ds合并到数据集ds1,都可以
            dataGridView1.DataSource = ds1.Tables[0];                               //dataGridView1的数据源是ds1
            /*dataGridView1.DataSource = ds.Tables[0]; */                           //测试
        }
    }
}

2.生成效果

二、 复制 DataSet 内容

为了在不影响原始数据的情况下使用数据,或者使用DataSet中数据的子集,可以创建DataSet的副本。当复制DataSet时,可以:

cs 复制代码
    ☑ 创建DataSet的原样副本,其中包含架构、数据、行状态信息和行版本。
    ☑ 创建包含现有DataSet的架构但仅包含已修改行的DataSet。可以返回已修改的所有行或者指定特定的DataRowState。
    ☑ 仅复制DataSet的架构(即关系结构),而不复制任何行

可以使用ImportRow将行导入现有的可以使用DataSet对象的Copy()方法创建包含架构和数据的DataSet的原样副本。Copy()方法的功能是复制指定DataSet的结构和数据。

cs 复制代码
    语法如下:
    public DataSet Copy()
    返回值:新的DataSet,具有与该DataSet相同的结构(表架构、关系和约束)和数据。

1.源码

cs 复制代码
//Form1.cs
//复制DataSet内容
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection conn;
        DataSet ds;

        /// <summary>
        /// 初始化Form1
        /// </summary>
        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "复制";

            //conn = new SqlConnection("Server=DESKTOP-QFENBNJ\\SQL_WEN;integrated security=SSPI;Initial Catalog=db_CSharp");
            conn = new SqlConnection("Server=DESKTOP-GFMO83R;integrated security=SSPI;Initial Catalog=db_CSharp");
            SqlCommand cmd = new SqlCommand("select * from tb_test", conn);//创建SqlCommand对象,建立数据集与SQL的关联
            SqlDataAdapter sda = new SqlDataAdapter     //创建数据适配器对象sda
            {
                SelectCommand = cmd
            };
            ds = new DataSet();                         //创建数据集ds
            sda.Fill(ds/*, "test"*/);                   //填充数据集ds,注释部分可删除
            dataGridView1.DataSource = ds.Tables[0];    //dataGridView1的数据源=ds
        }

        /// <summary>
        /// 复制数据集DataSet
        /// </summary>
        private void Button1_Click(object sender, EventArgs e)
        {
            DataSet ds1 = ds.Copy();                    //复制ds到ds1
            dataGridView2.DataSource = ds1.Tables[0];   //dataGridView2的数据源=ds1
        }
    }
}

2.生成效果

相关推荐
Rverdoser12 分钟前
【SQL】多表查询案例
数据库·sql
Galeoto15 分钟前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)43 分钟前
MySQL主从架构
服务器·数据库·mysql
leegong2311144 分钟前
学习PostgreSQL专家认证
数据库·学习·postgresql
喝醉酒的小白1 小时前
PostgreSQL:更新字段慢
数据库·postgresql
敲敲敲-敲代码1 小时前
【SQL实验】触发器
数据库·笔记·sql
和道一文字yyds1 小时前
MySQL 中的索引数量是否越多越好?为什么?如何使用 MySQL 的 EXPLAIN 语句进行查询分析?MySQL 中如何进行 SQL 调优?
数据库·sql·mysql
落笔画忧愁e2 小时前
FastGPT快速将消息发送至飞书
服务器·数据库·飞书
Σίσυφος19002 小时前
halcon 条形码、二维码识别、opencv识别
前端·数据库
小刘|2 小时前
深入理解 SQL 注入漏洞及解决方案
数据库·sql