ASP.NET蔚昜璃唗蹈趙傖Binary揣湔祫DB or File_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:在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發展而來。它

      來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
      相關.Net教程