程序构架搭建入门

数据的保存技术

使用文档进行数据保存

问题

  1. 当对象属性发生变化时,需要增加或减少信息的写入和读取次数

  2. 信息的安全性较差

序列化和反序列化

注意:

  1. 只要是对象皆可进行序列化和反序列化操作

  2. 如果某个数据对象要进行序列化和反序列化操作,首先要将这个对象进行添加特性-序列化标识

对象数据进行序列化保存

 private void btnSavesl_Click(object sender, EventArgs e)
        {
            //首先数据封装
            objStudent student = new objStudent()
            {
                ID = int.Parse(txtID.Text.Trim()),
                Name = txtName.Text.Trim(),
                Age = int.Parse(txtAge.Text.Trim()),
                Birthday = txtBirthday.Text.Trim()
            };
            //【1】创建文件流
            FileStream fs = new FileStream("Student.stu",FileMode.Create);
            //【2】创建二进制格式化器
            BinaryFormatter formatter = new BinaryFormatter();
            //【3】调用这个格式化器的序列化方法
            formatter.Serialize(fs,student);
            fs.Close();
        }

对象数据反序列化读取

 private void btnReadersl_Click(object sender, EventArgs e)
        {
            //【1】创建文件流
            FileStream fs = new FileStream("Student.stu", FileMode.Open);
            //【2】创建二进制格式化器
            BinaryFormatter formatter = new BinaryFormatter();
            //【3】调用反序列化方法
            objStudent student=(objStudent)formatter.Deserialize(fs);
            fs.Close();
            txtID.Text = student.ID.ToString();
            txtName.Text = student.Name;
            txtAge.Text = student.Age.ToString();
            txtBirthday.Text = student.Birthday;
        }

应用场合

  1. 应用程序配置信息(如果信息量较大,则使用该方法更方便读取和写入)

  2. 不需要数据库的时候,可以作为数据存取的载体

  3. WebServerice等服务中对象的传递

  4. 模块之间的数据传递

    https://www.cnblogs.com/jpfss/p/8397596.html

好处

  1. 对象数据的存取更方便,扩展性强

  2. 数据安全性更强

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值