论坛配置的数据在WEB上存储是以HashTable方式来存储,但是在数据库中却已一个字段来存储这个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;
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;
{
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);
//
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;
//
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();
//
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
binaryFormatter = null;
ms = 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);
//
ms.Write(b, 0, b.Length);
// Set the memory stream position to the beginning of the stream
//
ms.Position = 0;
//
ms.Position = 0;
// Set the internal hashtable
//
settings.Settings = (Hashtable) binaryFormatter.Deserialize(ms);
//
settings.Settings = (Hashtable) binaryFormatter.Deserialize(ms);
// Set the SiteID
//
settings.SiteID = (int) dr["SiteID"];
settings.SiteDomain = (string) dr["Application"];
settings.ForumsDisabled = (bool)dr["ForumsDisabled"];
//
settings.SiteID = (int) dr["SiteID"];
settings.SiteDomain = (string) dr["Application"];
settings.ForumsDisabled = (bool)dr["ForumsDisabled"];
return settings;
}
}