In Forums, some content is unfained, such as user information, in addition to some basic information, there may be some other information information, such as MSN, personal home page, signature file, etc., generally each attribute is corresponding to each property. A field in the database. But if we will increase some attributes, such as QQ numbers, blog addresses, if you still use this method of adding a data table field, you will change the program of database table structure, stored procedures, database access.
Maybe you have also encountered similar problems, how to use .NET's serialization and reverse selecente: For example, I need to add QQ number in user information, then I just need to add in the user class. An attribute public string qqim {get {return getExtendedAdAttribute ("qqim");} set {setExtendedAttribute ("QQIM", value);}} Do not need to modify the database table structure, do not need to modify the stored procedure, even the database access is not Need to move.
The main code of its specific implementation:
//
First create a new NameValueCollection object in the User class, saving these extensions in the NameValueCollection object
NameValueCollection ExtendedAttributes
=
New
NameValuecollection ();
//
Record from the NameValueCollection collection
public
String
GetExtendedAttribute
String
Name)
{String ReturnValue = extendedattributes [name]; if (returnvalue == null) Return string.empty; else returnvalue;}
//
Set the key values and values in the NameValueCollection of the extended properties
public
Void
SetExtendedAttribute (
String
Name,
String
Value
{ExtendedAttributes [name] = value;
//
Sequence is serialized to memory streams for memory streaming
//
Can be used to save to the database
public
Byte
SerializeExtendedAttributes ()
{// Serialized object binaryformatter binaryformatter = new binaryformatter (); // Create a memory stream, saved after serialization in MemoryStream ms = new memoryStream (); byte [] b; // Put the ExtendedAttributes object (save all inside User extended information) Sequence is used as memory stream // binaryformatter.serialize (ms, extendedattributes); // Set the starting position of the memory current // ms.position = 0; // Read to Byte array // b = new BYTE [MS.LENGTH]; MS.READ (B, 0, B.Length); ms.close (); Return B;} //
Defense sequenced ExtendedAttributes object content
//
From the database
public
Void
DeserializeExtendedAdattributes
Byte
SerializedExtendedAttributes)
{If (serializedExtendedAttributes.Length == 0) return; try {BinaryFormatter binaryFormatter = new BinaryFormatter (); MemoryStream ms = new MemoryStream (); // byte array to the memory stream // ms.Write (serializedExtendedAttributes, 0, serializedExtendedAttributes. Length); // Put the position of the memory to the start position // ms.position = 0; // Deserved into a NameValueCollection object, create a copy of the original object exactly the same copy // extendedattributes = (NameValueCollection) binaryformatter.deSerialize MS); ms.close ();} catch {}}
Substant serialization mechanisms are converted to a general (ie, continuous) byte stream, and then save the stream to a field of the database (one field in the FORUMS_USERPROFILE table in the database "StringNameValues VARBINARY (7500) "). When the read process is reorganized to the object, the identical copy of the original object is created.
Note that the general such attribute cannot be retrieved in the database, and to be serialized.