A way of storing and reading in asp.net forums

Source: Internet
Author: User
asp.net today found a special storage idea for me in asp.net forums, which is to serialize multiple fields into the database as an image by BinaryFormatter, and then read them again by converting them into memory streams. This is very handy when you need to store multiple fields. You don't have to write a long list of variable assignments.
First Look, an instance class AspNetForums.Components.SiteSettings () corresponding to the Admin Settings page.

The Sitesettings () defines the
Hashtable settings = new Hashtable ();
Next, a number of attributes are defined, corresponding to the fields required on the page, which correspond directly to Hashtable for example:
public int Sitesettingscachewindowinminutes {
get {
string key = "Sitesettingscachewindowinminutes";

if (Settings[key]!= null)
return (int) Settings[key];
Else
return 15;
}
set {
settings["sitesettingscachewindowinminutes"] = value;
}
}

That is, he is storing the entity field content in a Hashtable way, unlike the attributes we often use to represent it.
Now let's see how he put the Hashtable into the database.
The Sqldataprovider class has the following definitions
public override void Savesitesettings (Sitesettings sitesettings) {
Defines an instance of an image serialization
BinaryFormatter BinaryFormatter = new BinaryFormatter ();
Define a memory stream instance
MemoryStream ms = new MemoryStream ();
Byte[] B;

using (SqlConnection connection = Getsqlconnection ()) {
SqlCommand command = new SqlCommand (This.databaseowner + ". Forums_sitesettings_save", connection);
Command.commandtype = CommandType.StoredProcedure;


Deserializes the memory stream, Sitesettings.settings contains all the header fields in the memory stream
Binaryformatter.serialize (MS, sitesettings.settings);

Reset the current position of the memory stream
Ms. Position = 0;

b = New Byte[ms. Length];
To write a memory stream into B
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;
To be stored as images in a database
Command. Parameters.Add ("@Settings", SqlDbType.VarBinary, 8000). Value = b;

Open the connection and Exectute
//
Connection. Open ();
Command. ExecuteNonQuery ();
Connection. Close ();

}

BinaryFormatter = null;
ms = NULL;
}
It's simple! No more arguments, just fill in the Hashtable attribute, and then deserialize it into an image into the memory stream, then write the byte structure, and finally write the struct instance as a binary image stream to the database
A word! Convenient:
It's also easy to read these values out of the database, just
BinaryFormatter BinaryFormatter = new BinaryFormatter ();
The entity classes mentioned above
Sitesettings settings = new Sitesettings ();
MemoryStream ms = new MemoryStream ();
Byte[] B;
Dr is a DataReader
b = (byte[]) dr["Settings"];

Write stream
Ms. Write (b, 0, b.length);

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

Deserializes the memory stream and returns to Hashtable
Settings. Settings = (Hashtable) binaryformatter.deserialize (MS);
Summary: This approach should be suitable for simultaneous multiple fields to the database, but not very well tracked.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.