首页 > 编程 > .NET > 正文

ASP.NET蔚璃蹈Binary揣湔DB or File_.Net教程

2024-07-10 12:50:44
字体:
来源:转载
供稿:网友

推荐:在ASP.NET Atlas中调用Web Service
Atlas Framework中包含了对AJAX调用的封装,让您可以很方便的在客户端通过JavaScript调用服务器端方法。在本篇文章中,我将解释一下如何使用Atlas调用服务器端Web Service。使用Atlas,我们

衄?蹦跺..苤竭跺陲..憩善埭..湮模?睡蔚璃蹈Binary Data..?揣湔善蹋踱紫孬..
?褫蹈..憩隅褫蹈...?妗蹈祥隅猁Binary Data..珩褫XML or SOAP Data腔...

忑珂掘跺DB,凳?ㄩ

懂憩

腔?ㄩ

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

}

硒ㄩ

分享:ASP.NET中MD5和SHA1加密的几种方法
MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest开发出来,经md2、md3和md4发展而来。它

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表