Storage and reading ideas in ASP.NET Forums

xiaoxiao2021-03-06  74

Today, I found a special storage idea for me today, which is serialized by binaryformatter, as an image storage into the database, and then read it by converting into memory flow, This approach is very convenient for the need to store multiple fields. Don't write a longer string of variable assignment. First take a look, an instance class corresponding to the management settings page ASPNETFORUMS.COMPONENTS.SITESETTINGS ()

In SiteSettings () defines HashTable settings = new hashtable (); Next, a lot of properties are defined, respectively correspond to the fields required in the page, these attributes correspond directly to HashTable, for example: public int sitesttingscacheWindowinMinUtes {Get {string key = "SitesettingScacheWindowinMinUtes";

IF (settings [key]! = null) return (int) settings [key]; else return 15;} set {settings ["suitsettingscachewindowin machs"] = value;}}

That is to say, use the HashTable method in the storage entity field content, different from our frequent use of properties. Next, see how he puts this HashTable content in the SQLDataProvider class in the database into the SQLDATAPROIDER class, as follows: Public override void SavesiteSettings (Sitesetting Sitesettings) {// Defines an image serialization instance binaryformatter binaryformatter = new binaryformatter (); // Defines a memory current 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;

// Reverse the memory flow in SiteSettings.Settings contains all title fields in memory flow in memory flows binaryformatter.Serialize (MS, Sitesettings.Settings);

// Reset memory current position ms.position = 0; b = new byte [ms.length]; // Write memory flow to 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; // is used as an image to store the 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;} is simple! No need to pass multiple parameters, only to fill in the HashTable property first, then fill the memory flow in the image, then write the Byte structure, Finally, this structure instance is written into a database as a binary image stream! Convenient :) It is easy to read these values ​​from the database, only binaryformatter binaryformatter = new binaryformatter (); // The entity class Sitsettings settings mentioned above = New Sitesettings (); MemoryStream MS = New MemoryStream (); Byte [] B; // DR is a DataReader B = (Byte []) DR ["settings"];

// Write the stream MS.Write (B, 0, B.Length);

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

// Reverse the memory flow in the memory and return to HashTable Settings.Settings = (Hashtable) binaryformatter.deSerialize (ms); summary: This method should be applied to multiple fields to deposit into the database, but not well tracked.

转载请注明原文地址:https://www.9cbs.com/read-121348.html

New Post(0)