序列化:将对象转换为二进制 object->二进制 可以写到硬盘文件也可以存到内存文件文件里 。流
反序列化:将二进制数据转换为对象
断点:窗口-即时窗口找文件存的东西
先添加命名空间:
using System.Runtime.Serialization.Formatters.Binary;序列化。格式化。二进制
using System.IO;流
反序列化:将二进制数据转换为对象
断点:窗口-即时窗口找文件存的东西
先添加命名空间:
using System.Runtime.Serialization.Formatters.Binary;序列化。格式化。二进制
using System.IO;流
//指示一个类可以序列化,但是无法继承此类
[Serializable]
public class MES
{
public string Code;
public string Name;
public string Mainna()
{
return Name + Code + "wored";
}
}
[Serializable]
public class MES
{
public string Code;
public string Name;
public string Mainna()
{
return Name + Code + "wored";
}
}
//序列化
protected void Button1_Click(object sender, EventArgs e)
{
//一:造一个对象
MES data = new MES();
data.Code = "008";
data.Name = "壮族";
//二:将对象写到文件中
//1.找到文件路径,并改为绝对路径,准备一个文件流
string path = Server.MapPath("wenjian/aa.txt");
//文件有就打开。没有就创建
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
//2.序列化对象
BinaryFormatter bf = new BinaryFormatter();
//流,加一个对象
bf.Serialize(fs, data);
//流要最后关闭
fs.Close();
}
//反序列化
protected void Button2_Click(object sender, EventArgs e)
{
//找到文件路径,并转化为绝对路径
string path = Server.MapPath("wenjian/aa.txt");
//造一个文件流
FileStream fs = new FileStream(path,FileMode.Open);
//饭序列化
BinaryFormatter bf = new BinaryFormatter();
MES data= bf.Deserialize(fs)as MES ;
Label1.Text = data.Mainna();
}
protected void Button1_Click(object sender, EventArgs e)
{
//一:造一个对象
MES data = new MES();
data.Code = "008";
data.Name = "壮族";
//二:将对象写到文件中
//1.找到文件路径,并改为绝对路径,准备一个文件流
string path = Server.MapPath("wenjian/aa.txt");
//文件有就打开。没有就创建
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
//2.序列化对象
BinaryFormatter bf = new BinaryFormatter();
//流,加一个对象
bf.Serialize(fs, data);
//流要最后关闭
fs.Close();
}
//反序列化
protected void Button2_Click(object sender, EventArgs e)
{
//找到文件路径,并转化为绝对路径
string path = Server.MapPath("wenjian/aa.txt");
//造一个文件流
FileStream fs = new FileStream(path,FileMode.Open);
//饭序列化
BinaryFormatter bf = new BinaryFormatter();
MES data= bf.Deserialize(fs)as MES ;
Label1.Text = data.Mainna();
}
班级座次表
public partial class banji : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//序列化存到数据库用内存流
protected void Button1_Click(object sender, EventArgs e)
{
//造一个数组,存内存流中的数据
byte[] sj = null;
//
//造嘻哈表集合 HashSet<string> 一个类型的函数
Dictionary<string, string> list = new Dictionary<string, string>();
// 遍历在这个页面中的控件,用哈希表集合 key和value 值正好对应
foreach (Control ctr in this.Page.Controls)
{
//is 这个对象是不是属于这个类
if (ctr is TextBox)
{
TextBox txt = ctr as TextBox;
list.Add(txt.ID, txt.Text);
}
}
//造一个流 不早硬盘上些东西就用内存流,若是想硬盘上存东西用文件流
MemoryStream ms = new MemoryStream();
//序列化
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, list);
//将内存流中的数据传到数组
ms.Seek(0, SeekOrigin.Begin);//偏移量,从哪个位置开始便宜
sj = ms.ToArray();
XuLie data = new XuLie();
data.Name = "班级信息";
data.Content = sj;//Content的数据类型为image
//存到数据库
TestDataContext context = new TestDataContext();
context.XuLie.InsertOnSubmit(data);
context.SubmitChanges();
////造一个数组
//byte[] sj = null;
////造哈希表集合
//Dictionary<string, string> list = new Dictionary<string, string>();
////用哈希表集合存储内容key 和value 是对应的
//foreach (Control ctl in this.form1.Controls)
//{
// if (ctl is TextBox)
// {
// TextBox txt = ctl as TextBox;
// list.Add(txt.ID, txt.Text);
// }
//}
////造一个内存流
//MemoryStream ms = new MemoryStream();
////序列化
//BinaryFormatter bf = new BinaryFormatter();
//bf.Serialize(ms, list);
////将内存流中的数据传给数组
//sj = ms.ToArray();
//XuLie data = new XuLie();
//data.Name = "班级信息";
//data.Content = sj;
////存到数据库
//TestDataContext context = new TestDataContext();
//context.XuLie.InsertOnSubmit(data);
//context.SubmitChanges();
//ms.Close();
}
//饭序列化取出数据
protected void Button2_Click(object sender, EventArgs e)
{
//找出数据
TestDataContext context = new TestDataContext();
XuLie data = context.XuLie.Where(p => p.Ids == 2).First();
byte[] sj = data.Content.ToArray();
//流
MemoryStream ms = new MemoryStream();
if (sj != null)
{
ms.Write(sj, 0, sj.Length);
}
ms.Seek(0, SeekOrigin.Begin);
//反序列化
BinaryFormatter bf = new BinaryFormatter();
Dictionary<string, string> list = bf.Deserialize(ms) as Dictionary<string, string>;
//显示找具体的form表单或者是加一个 panel 找panel的模板
foreach (Control ctl in this.form1.Controls)
{
if (ctl is TextBox)
{
TextBox txt = ctl as TextBox;
txt.Text = list[txt.ID].ToString();
}
}
ms.Close();
{
protected void Page_Load(object sender, EventArgs e)
{
}
//序列化存到数据库用内存流
protected void Button1_Click(object sender, EventArgs e)
{
//造一个数组,存内存流中的数据
byte[] sj = null;
//
//造嘻哈表集合 HashSet<string> 一个类型的函数
Dictionary<string, string> list = new Dictionary<string, string>();
// 遍历在这个页面中的控件,用哈希表集合 key和value 值正好对应
foreach (Control ctr in this.Page.Controls)
{
//is 这个对象是不是属于这个类
if (ctr is TextBox)
{
TextBox txt = ctr as TextBox;
list.Add(txt.ID, txt.Text);
}
}
//造一个流 不早硬盘上些东西就用内存流,若是想硬盘上存东西用文件流
MemoryStream ms = new MemoryStream();
//序列化
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, list);
//将内存流中的数据传到数组
ms.Seek(0, SeekOrigin.Begin);//偏移量,从哪个位置开始便宜
sj = ms.ToArray();
XuLie data = new XuLie();
data.Name = "班级信息";
data.Content = sj;//Content的数据类型为image
//存到数据库
TestDataContext context = new TestDataContext();
context.XuLie.InsertOnSubmit(data);
context.SubmitChanges();
////造一个数组
//byte[] sj = null;
////造哈希表集合
//Dictionary<string, string> list = new Dictionary<string, string>();
////用哈希表集合存储内容key 和value 是对应的
//foreach (Control ctl in this.form1.Controls)
//{
// if (ctl is TextBox)
// {
// TextBox txt = ctl as TextBox;
// list.Add(txt.ID, txt.Text);
// }
//}
////造一个内存流
//MemoryStream ms = new MemoryStream();
////序列化
//BinaryFormatter bf = new BinaryFormatter();
//bf.Serialize(ms, list);
////将内存流中的数据传给数组
//sj = ms.ToArray();
//XuLie data = new XuLie();
//data.Name = "班级信息";
//data.Content = sj;
////存到数据库
//TestDataContext context = new TestDataContext();
//context.XuLie.InsertOnSubmit(data);
//context.SubmitChanges();
//ms.Close();
}
//饭序列化取出数据
protected void Button2_Click(object sender, EventArgs e)
{
//找出数据
TestDataContext context = new TestDataContext();
XuLie data = context.XuLie.Where(p => p.Ids == 2).First();
byte[] sj = data.Content.ToArray();
//流
MemoryStream ms = new MemoryStream();
if (sj != null)
{
ms.Write(sj, 0, sj.Length);
}
ms.Seek(0, SeekOrigin.Begin);
//反序列化
BinaryFormatter bf = new BinaryFormatter();
Dictionary<string, string> list = bf.Deserialize(ms) as Dictionary<string, string>;
//显示找具体的form表单或者是加一个 panel 找panel的模板
foreach (Control ctl in this.form1.Controls)
{
if (ctl is TextBox)
{
TextBox txt = ctl as TextBox;
txt.Text = list[txt.ID].ToString();
}
}
ms.Close();