📄 sample9.cs
字号:
namespace apiBook
{
using System;
using System.Data;
using System.IO;
public class TestDataSetClass
{
public static void Main()
{
TestDataSetClass test=new TestDataSetClass(); DataTable testDT=new DataTable("student");
DataSet testDSA = new DataSet("TestDataSet"); testDSA.Tables.Add(testDT);
DataColumn testDC;
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.Int32"); testDC.ColumnName="Id";
testDT.Columns.Add(testDC);
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.String");
testDC.ColumnName = "Name";
testDT.Columns.Add(testDC);
testDC = new DataColumn();
testDC.DataType = Type.GetType("System.String");
testDC.ColumnName = "School";
testDT.Columns.Add(testDC);
DataRow testDR=testDT.NewRow();
testDR["Id"]=1;
testDR["Name"]="Rose";
testDR["School"]="SCUT";
testDT.Rows.Add(testDR);
testDR=testDT.NewRow();
testDR["Id"]=2;
testDR["Name"]="Coke";
testDR["School"]="SCNU";
testDT.Rows.Add(testDR);
testDSA.AcceptChanges();
//使用AcceptChanges方法提交自加载此DataSet进行的所有更改
Console.WriteLine("原始数据表:");
test.DoPrint(testDSA);
Console.WriteLine();
Console.WriteLine("当前数据");
testDR = testDT.NewRow();
testDR[0] = 3;
testDR[1] = "Mike";
testDR[2]="SCUT";
testDT.Rows.Add(testDR);
testDR = testDT.NewRow();
testDR[0] = 4;
testDR[1] = "Clin";
testDR[2]="SCNU";
testDT.Rows.Add(testDR);
test.DoPrint(testDSA);
Console.WriteLine();
Console.WriteLine("该表数据是否被更改过?"+testDSA.HasChanges());
//使用HasChanges方法判断DataSet对象是否有更改
DataSet testDSB=testDSA.GetChanges();
//使用GetChanges方法获取DataSet的副本
//该副本包含自上次加载以来或自调用AcceptChanges以来对该数据集进行的所有更改
Console.WriteLine("添加的数据:");
test.DoPrint(testDSB);
testDSB.Clear();
//使用Clear方法清除所有数据
Console.WriteLine("执行Clear操作后");
test.DoPrint(testDSB);
Console.WriteLine();
testDSB=new DataSet();
DataTable testDT1=testDT.Clone();
//使用Clone方法复制DataSet的结构
testDR = testDT1.NewRow();
testDR[0] = 8;
testDR[1] = "Clin";
testDR[2]="SCNU";
testDT1.Rows.Add(testDR);
testDR = testDT1.NewRow();
testDR[0] = 9;
testDR[1] = "LiZe";
testDR[2]="SCUT";
testDT1.Rows.Add(testDR);
testDSB.Tables.Add(testDT1);
Console.WriteLine("一个新的DataSet对象:");
test.DoPrint(testDSB);
Console.WriteLine();
testDSA.Merge(testDSB);
//使用Merge方法合并两个DataSet对象
Console.WriteLine("两个DataSet对象合并后");
test.DoPrint(testDSA);
Console.WriteLine();
Console.WriteLine("将testDSA恢复原始状态:");
testDSA.Reset();
//使用Reset方法将DataSet重置为其初始状态
test.DoPrint(testDSA);
Console.ReadLine();
}
public void DoPrint(DataSet ds)
{
foreach(DataTable dt in ds.Tables)
{
Console.WriteLine(" " + dt.TableName+"表");
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dt.Columns)
{
Console.Write(" " + dr[dc] );
}
Console.WriteLine();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -