惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
GbyAI
GbyAI
P
Proofpoint News Feed
B
Blog
MyScale Blog
MyScale Blog
V
V2EX
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
量子位
Jina AI
Jina AI
博客园 - 叶小钗
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
罗磊的独立博客
L
LangChain Blog
I
InfoQ
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
小众软件
小众软件
V
Visual Studio Blog
月光博客
月光博客
The Cloudflare Blog
雷峰网
雷峰网

博客园 - Loviy.Net

从今天开始博客重新开张啦 [cnforums1.2代码学习]3、论坛的数据库操作流程 - Loviy.Net - 博客园 [cnforums1.2代码学习]2、关于论坛的页面结构 - Loviy.Net - 博客园 [cnforums1.2代码学习]1、论坛的整体结构 XSL的运算符 XSL学习四 - xsl:choose 学习XSL - xsl:if 学习XSL二 - 学习XSL一 学习委托 客户短保存信息(cookie) SQL常用命令使用方法 Sql Server基本函数 常用SQL语句 WebLogic表格对象 Jsp通过JDBC连接SQL数据库 经典的鱼和水的对白 用样式定义打印1px表格 女孩子的心思真的难以琢磨
[cnforums1.2代码学习]4、关于论坛配置的序列化和反序列化做法
Loviy.Net · 2005-06-30 · via 博客园 - Loviy.Net

论坛配置的数据在WEB上存储是以HashTable方式来存储,但是在数据库中却已一个字段来存储这个HashTable
以序列化的方式将HashTable序列化为二进制数据存储到数据库中,需要的时候在来反序列化他。

HashTable类型是怎么序列化到数据库中的,代码如下:

//参照CnForums1.2\Data Providers\SqlDataProvider\SqlDataProvider.cs中代码
 public override void SaveSiteSettings(SiteSettings siteSettings)
 {
  BinaryFormatter binaryFormatter = new BinaryFormatter();
  MemoryStream ms = new MemoryStream();
  byte[] b;

 using( SqlConnection connection = GetSqlConnection() )
 {
   SqlCommand command = new SqlCommand(this.databaseOwner + ".forums_SiteSettings_Save", connection);
   command.CommandType = CommandType.StoredProcedure;

   // Serialize the SiteSettings
   //
   binaryFormatter.Serialize(ms, siteSettings.Settings);

   // Set the position of the MemoryStream back to 0
   //
   ms.Position = 0;
         
   // Read in the byte array
   //
   b = new Byte[ms.Length];
   ms.Read(b, 0, b.Length);

   // Set the parameters
   //
   command.Parameters.Add("@Application", SqlDbType.NVarChar,512).Value = siteSettings.SiteDomain;
   command.Parameters.Add("@ForumsDisabled", SqlDbType.SmallInt).Value = siteSettings.ForumsDisabled;
   command.Parameters.Add("@Settings", SqlDbType.VarBinary, 8000).Value = b;

   // Open the connection and exectute
   //
   connection.Open();
   command.ExecuteNonQuery();
  connection.Close();

 }

 binaryFormatter = null;
 ms = null;
}

如何取数据库中序列化的数,则应通过反序列化
反序列化的代码:
//
//参照CnForums1.2\Components\Provider\ForumsDataProvider.cs文件
//
public static SiteSettings PopulateSiteSettingsFromIDataReader(IDataReader dr)
{
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        SiteSettings settings = new SiteSettings();
        MemoryStream ms = new MemoryStream();
        Byte[] b;

        b = (byte[]) dr["Settings"];

        // Read the byte array into a memory stream
        //
        ms.Write(b, 0, b.Length);

        // Set the memory stream position to the beginning of the stream
        //
        ms.Position = 0;

        // Set the internal hashtable
        //
        settings.Settings = (Hashtable) binaryFormatter.Deserialize(ms);

        // Set the SiteID
        //
        settings.SiteID = (int) dr["SiteID"];
        settings.SiteDomain = (string) dr["Application"];
    settings.ForumsDisabled = (bool)dr["ForumsDisabled"];

        return settings;
}