眕狟峈竘蚚腔囀?ㄩ
asp.net(c#)
SerializingObjects.aspx
view plaincopy to clipboardprint? <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SerializingObjects.aspx.cs" Inherits="SerializingObjects" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Serializing Objects</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="ddlType" runat="server"> <asp:ListItem Value="0">訧蹋踱</asp:ListItem> <asp:ListItem Value="1">紫黓</asp:ListItem> </asp:DropDownList> <asp:Button ID="btnLoad" runat="server" OnClick="btnLoad_Click" Text="黍?" /> <asp:Label ID="Label1" runat="server" Text="晤瘍ㄩ"></asp:Label> <asp:TextBox ID="txbId" runat="server"></asp:TextBox> <asp:Label ID="Label2" runat="server" Text="靡備ㄩ"></asp:Label> <asp:TextBox ID="txbName" runat="server"></asp:TextBox> <asp:Label ID="Label3" runat="server" Text="萇趕ㄩ"></asp:Label> <asp:TextBox ID="txbTel" runat="server"></asp:TextBox> <asp:Label ID="Label4" runat="server" Text="華硊ㄩ"></asp:Label> <asp:TextBox ID="txbAddress" runat="server"></asp:TextBox> <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="陔崝" /></div> </form> </body> </html> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SerializingObjects.aspx.cs" Inherits="SerializingObjects" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Serializing Objects</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="ddlType" runat="server"> <asp:ListItem Value="0">訧蹋踱</asp:ListItem> <asp:ListItem Value="1">紫黓</asp:ListItem> </asp:DropDownList> <asp:Button ID="btnLoad" runat="server" OnClick="btnLoad_Click" Text="黍?" /><br /> <asp:Label ID="Label1" runat="server" Text="晤瘍ㄩ"></asp:Label> <asp:TextBox ID="txbId" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label2" runat="server" Text="靡備ㄩ"></asp:Label> <asp:TextBox ID="txbName" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label3" runat="server" Text="萇趕ㄩ"></asp:Label> <asp:TextBox ID="txbTel" runat="server"></asp:TextBox><br /> <asp:Label ID="Label4" runat="server" Text="華硊ㄩ"></asp:Label> <asp:TextBox ID="txbAddress" runat="server"></asp:TextBox> <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="陔崝" /></div> </form> </body> </html>
SerializingObjects.aspx.cs
view plaincopy to clipboardprint? using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; using System.Runtime.Serialization.Formatters.Binary; public partial class SerializingObjects : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnAdd_Click(object sender, EventArgs e) { if (this.ddlType.SelectedIndex == 0) { SaveToDB(); } else { SaveToFile(); } } //湔祫訧蹋踱(Binary Serialization) protected void SaveToDB() { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { using (MemoryStream ms = new MemoryStream()) { conn.Open(); Member mem = new Member(); mem.Id = this.txbId.Text; mem.Name = this.txbName.Text; mem.Tel = this.txbTel.Text; mem.Address = this.txbAddress.Text; BinaryFormatter b = new BinaryFormatter(); b.Serialize(ms, mem); ms.Seek(0, 0); string sql = "insert into member values(@data)"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@data", SqlDbType.Binary).Value = ms.ToArray(); cmd.ExecuteNonQuery(); } } } //蚕訧蹋踱黍?(Binary Serialization) protected void LoadFromDB() { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { using (MemoryStream ms = new MemoryStream()) { conn.Open(); string sql = "select data from member"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); adapter.Fill(dt); if (dt.Rows.Count > 0) { byte[] buffer = (byte[])dt.Rows[dt.Rows.Count-1][0]; ms.Write(buffer, 0, buffer.Length); ms.Seek(0, 0); BinaryFormatter b = new BinaryFormatter(); Member mem = new Member(); mem = (Member)b.Deserialize(ms); this.txbId.Text = mem.Id; this.txbName.Text = mem.Name; this.txbTel.Text = mem.Tel; this.txbAddress.Text = mem.Address; } } } } //湔祫紫黓(Binary Serialization) protected void SaveToFile() { string path = Server.MapPath("member.dat"); using (Stream s = File.Open(path, FileMode.Create)) { Member mem = new Member(); mem.Id = this.txbId.Text; mem.Name = this.txbName.Text; mem.Tel = this.txbTel.Text; mem.Address = this.txbAddress.Text; BinaryFormatter b = new BinaryFormatter(); b.Serialize(s, mem); } } //蚕紫黓黍?(Binary Serialization) protected void LoadFromFile() { string path = Server.MapPath("member.dat"); if (File.Exists(path)) { using (Stream s = File.Open(path, FileMode.Open)) { BinaryFormatter b = new BinaryFormatter(); Member mem = new Member(); mem = (Member)b.Deserialize(s); this.txbId.Text = mem.Id; this.txbName.Text = mem.Name; this.txbTel.Text = mem.Tel; this.txbAddress.Text = mem.Address; } } } protected void btnLoad_Click(object sender, EventArgs e) { if (this.ddlType.SelectedIndex == 0) { LoadFromDB(); } else { LoadFromFile(); } } } [Serializable] public class Member { private string _id = string.Empty; private string _name = string.Empty; private string _tel = string.Empty; private string _address = string.Empty; public Member() { } public Member(string id, string name,string tel,string address) { _id = id; _name = name; _tel = tel; _address = address; } public string Id { get { return _id; } set { _id = value; } } public string Name { get { return _name; } set { _name = value; } } public string Tel { get { return _tel; } set { _tel = value; } } public string Address { get { return _address; } set { _address = value; } } } using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; using System.Runtime.Serialization.Formatters.Binary;
public partial class SerializingObjects : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
} protected void btnAdd_Click(object sender, EventArgs e) { if (this.ddlType.SelectedIndex == 0) { SaveToDB(); } else { SaveToFile(); } }
//湔祫訧蹋踱(Binary Serialization) protected void SaveToDB() { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { using (MemoryStream ms = new MemoryStream()) { conn.Open();
Member mem = new Member(); mem.Id = this.txbId.Text; mem.Name = this.txbName.Text; mem.Tel = this.txbTel.Text; mem.Address = this.txbAddress.Text;
BinaryFormatter b = new BinaryFormatter(); b.Serialize(ms, mem); ms.Seek(0, 0);
string sql = "insert into member values(@data)"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@data", SqlDbType.Binary).Value = ms.ToArray(); cmd.ExecuteNonQuery();
} } }
//蚕訧蹋踱黍?(Binary Serialization) protected void LoadFromDB() { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { using (MemoryStream ms = new MemoryStream()) { conn.Open();
string sql = "select data from member"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); adapter.Fill(dt);
if (dt.Rows.Count > 0) { byte[] buffer = (byte[])dt.Rows[dt.Rows.Count-1][0]; ms.Write(buffer, 0, buffer.Length); ms.Seek(0, 0); BinaryFormatter b = new BinaryFormatter(); Member mem = new Member(); mem = (Member)b.Deserialize(ms);
this.txbId.Text = mem.Id; this.txbName.Text = mem.Name; this.txbTel.Text = mem.Tel; this.txbAddress.Text = mem.Address; } } } }
//湔祫紫黓(Binary Serialization) protected void SaveToFile() { string path = Server.MapPath("member.dat"); using (Stream s = File.Open(path, FileMode.Create)) { Member mem = new Member(); mem.Id = this.txbId.Text; mem.Name = this.txbName.Text; mem.Tel = this.txbTel.Text; mem.Address = this.txbAddress.Text;
BinaryFormatter b = new BinaryFormatter(); b.Serialize(s, mem); } }
//蚕紫黓黍?(Binary Serialization) protected void LoadFromFile() { string path = Server.MapPath("member.dat"); if (File.Exists(path)) { using (Stream s = File.Open(path, FileMode.Open)) { BinaryFormatter b = new BinaryFormatter(); Member mem = new Member(); mem = (Member)b.Deserialize(s);
this.txbId.Text = mem.Id; this.txbName.Text = mem.Name; this.txbTel.Text = mem.Tel; this.txbAddress.Text = mem.Address; } } }
protected void btnLoad_Click(object sender, EventArgs e) { if (this.ddlType.SelectedIndex == 0) { LoadFromDB(); } else { LoadFromFile(); } } }
[Serializable] public class Member { private string _id = string.Empty; private string _name = string.Empty; private string _tel = string.Empty; private string _address = string.Empty;
public Member() {
}
public Member(string id, string name,string tel,string address) { _id = id; _name = name; _tel = tel; _address = address; }
public string Id { get { return _id; } set { _id = value; } }
public string Name { get { return _name; } set { _name = value; } }
public string Tel { get { return _tel; } set { _tel = value; } }
public string Address { get { return _address; } set { _address = value; } }
}
|