首页 > 代码库 > DataSet与二进制文件和XML文件

DataSet与二进制文件和XML文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
1.将DataSet对象序列化到二进制文件
2.将二进制文件序列化到DataSet对象
3.将DataSet中的数据写到XML文件
4.将数据XML文件读到DataSet对象
5.将DataSet中的数据及架构写到XML文件
6.将数据及架构XML文件读到DataSet对象
7.将DataSet中的数据及架构分别写到不同的XML文件
8.将相关的数据及架构XML文件读到DataSet对象
================================
 
// 1.将DataSet对象序列化到二进制文件
protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
    SqlDataAdapter myda = new SqlDataAdapter("select personName,personAge from person", myConn);
    DataSet myds = new DataSet();
    myConn.Open();
    myda.Fill(myds);
    myConn.Close();
    myds.RemotingFormat = SerializationFormat.Binary;
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    System.IO.StreamWriter swBinary = new System.IO.StreamWriter("D:\\DataSetBinary.dat");
    bf.Serialize(swBinary.BaseStream, myds);
    swBinary.Close();
}
 
// 2.将二进制文件序列化到DataSet对象
protected void Button2_Click(object sender, EventArgs e)
{
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    System.IO.FileStream fsBinary = new System.IO.FileStream("D:\\DataSetBinary.dat", System.IO.FileMode.Open);
    DataSet myds = (DataSet)bf.Deserialize(fsBinary);
    this.GridView2.DataSource = myds;
    this.GridView2.DataBind();
}
 
// 3.将DataSet中的数据写到XML文件
protected void Button3_Click(object sender, EventArgs e)
{
    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
    SqlDataAdapter myda = new SqlDataAdapter("select personName,personAge from person", myConn);
    DataSet myds = new DataSet();
    myConn.Open();
    myda.Fill(myds);
    myConn.Close();
    myds.WriteXml("D:\\DataSetXmlData.xml");
}
 
// 4.将数据XML文件读到DataSet对象
protected void Button4_Click(object sender, EventArgs e)
{
    DataSet myds = new DataSet();
    myds.ReadXml("D:\\DataSetXmlData.xml");
    this.GridView2.DataSource = myds;
    this.GridView2.DataBind();
}
 
// 5.将DataSet中的数据及架构写到XML文件
protected void Button5_Click(object sender, EventArgs e)
{
    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
    SqlDataAdapter myda = new SqlDataAdapter("select personName,personAge from person", myConn);
    DataSet myds = new DataSet();
    myConn.Open();
    myda.Fill(myds);
    myConn.Close();
    myds.WriteXml("D:\\DataSetXmlDataSchema.xml", XmlWriteMode.WriteSchema);
}
 
// 6.将数据及架构XML文件读到DataSet对象
protected void Button6_Click(object sender, EventArgs e)
{
    DataSet myds = new DataSet();
    myds.ReadXml("D:\\DataSetXmlDataSchema.xml",XmlReadMode.ReadSchema);
    this.GridView2.DataSource = myds;
    this.GridView2.DataBind();
}
 
// 7.将DataSet中的数据及架构分别写到不同的XML文件
protected void Button7_Click(object sender, EventArgs e)
{
    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
    SqlDataAdapter myda = new SqlDataAdapter("select personName,personAge from person", myConn);
    DataSet myds = new DataSet();
    myConn.Open();
    myda.Fill(myds);
    myConn.Close();
    myds.WriteXmlSchema("D:\\DataSetXmlSchemaOnly.xml");
    myds.WriteXml("D:\\DataSetXmlDataOnly.xml");
}
 
// 8.将相关的数据及架构XML文件读到DataSet对象
protected void Button8_Click(object sender, EventArgs e)
{
    DataSet myds = new DataSet();
    myds.ReadXmlSchema("D:\\DataSetXmlSchemaOnly.xml");
    myds.ReadXml("D:\\DataSetXmlDataSchema.xml");
    this.GridView2.DataSource = myds;
    this.GridView2.DataBind();
}