• DataSet与二进制文件和XML文件


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

  • 相关阅读:
    用c#控制台玩石头剪刀布游戏
    各种流行的编程风格
    New page, new world.
    windows phone注册表问题
    windows 7下用SaveFileDialog弹不出对话框问题
    在写windows phone自定义控件遇到的问题,请高手解答
    自定义事件和windows phone访问网站取得数据
    windows phone画板程序
    Sharepoint contant class
    How to nagivate to a list item by site id, web id,list id and item id.
  • 原文地址:https://www.cnblogs.com/freeliver54/p/755134.html
Copyright © 2020-2023  润新知