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();
}
|