⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample8.cs

📁 C#函数手册
💻 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);	
			testDR = testDT.NewRow();
			testDR[0] = 3;
			testDR[1] = "Mike";
			testDR[2]="SCUT";
			testDT.Rows.Add(testDR);
			testDSA.AcceptChanges();
			//使用AcceptChanges方法提交自加载此DataSet进行的所有更改
			Console.WriteLine("原始数据表:");
			test.DoPrint(testDSA);
			Console.WriteLine();
			Console.WriteLine("存储在该内存缓存的数据:");
			Console.WriteLine(testDSA.GetXml());
			//使用GetXml方法获取存储在DataSet中的数据的XML表示形式
			string xmlFN = "XmlDoc.xml";
			FileStream testFS = new FileStream(xmlFN,FileMode.Create);
			testDSA.WriteXml(testFS);
			//使用WriteXml方法通过Stream对象为DataSet写当前数据
			testFS.Close();
			testDSA.Dispose();
			DataSet testDSB = new DataSet("TestGetDataSet");
			testFS = new FileStream(xmlFN,FileMode.Open);
			testDSB.ReadXml(testFS);
			//使用ReadXml方法将XML架构和数据读入DataSet
			Console.WriteLine("从生成的XML文档读取的数据表:");
			test.DoPrint(testDSB);			
			testDSB.WriteXmlSchema("C:\\XmlSchDoc.xml");
			//使用WriteXmlSchema方法将XML架构形式的DataSet结构写入文件
			DataSet testDSC=new DataSet();
			testDSC.ReadXmlSchema("C:\\XmlSchDoc.xml");
			//使用ReadXmlSchema方法从指定的文件中将XML架构读入DataSet
			Console.WriteLine("获取的XSD架构如下:");
			Console.WriteLine(testDSC.GetXmlSchema());
			//使用GetXmlSchema方法获取存储在DataSet中的数据的XML表示形式的XSD架构
			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 + -