Do you think that it is not safe to store webparts in the database that comes with asp.net? Every time you create a project with WebPart, you must have asp.net deploy an aspnetdb database. It seems that the data of WebPart cannot be fully integrated into your system.
This article describes how to obtain personalized WebPart data from Asp.net and how to store the data.
1 In Asp. Net2.0, how does one access WebPart data?
In Asp. Net2.0SqlPersonalizationProviderClass to access data, mentionedSqlPersonalizationProvider, You have to mention his parent classPersonalizationProvider, PersonalizationProviderProvides some abstract methods of personalized data to store and read personalized data of WebPart. Of course, we can also buildMyPersonalizationProviderTo access our own personalized data.
This section focuses on
- SavePersonalizationBlob ()
//
// Summary:
// Save the original personalized settings data to the basic Microsoft SQL Server database.
//
// Parameters:
// WebPartManager:
// Manage System. Web. UI. WebControls. WebParts. WebPartManager for personalized settings.
//
// UserName:
// User name in the range of System. Web. UI. WebControls. WebParts. PersonalizationScope. User to use as the keyword for personalized settings.
//
// DataBlob:
// The byte array of the data to be saved.
//
// Path:
// Indicates the System. Web. UI. WebControls. WebParts. PersonalizationScope of the personalized settings to be saved. This value cannot be null.
protected abstract void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName, byte[] dataBlob);
The webPartManager parameter is the webPartManager on your page.
The path parameter indicates the page on which your WebPart is deployed, on Index. aspx or Default. aspx.
The userName parameter. if MemberShip is used, your UserName is returned.
The parameter dataBlob is WebPart personalized data, which can be stored in any Image field.
If you have such a data structure:
Id |
Page |
UserId |
DataBlob |
0 |
Index. aspx |
A01 |
******** |
1 |
Default. aspx |
A02 |
******** |
You can control the WebPart data in your own hands.
For more information, seeSqlPersonalizationProviderHow to store
SavePersonalizationBlob
Private void SavePersonalizationState (SqlConnection connection, string path, string userName, byte [] state)
{
SqlCommand command;
If (userName! = Null)
{
Command = new SqlCommand ("dbo. aspnet_PersonalizationPerUser_SetPageSettings", connection );
}
Else
{
Command = new SqlCommand ("dbo. aspnet_PersonalizationAllUsers_SetPageSettings", connection );
}
This. SetCommandTypeAndTimeout (command );
Command. Parameters. Add (this. CreateParameter ("@ ApplicationName", SqlDbType. NVarChar, this. ApplicationName ));
Command. Parameters. Add (this. CreateParameter ("@ Path", SqlDbType. NVarChar, path ));
Command. Parameters. Add (this. CreateParameter ("@ PageSettings", SqlDbType. Image, state ));
Command. Parameters. Add (this. CreateParameter ("@ CurrentTimeUtc", SqlDbType. DateTime, DateTime. UtcNow ));
If (userName! = Null)
{
Command. Parameters. Add (this. CreateParameter ("@ UserName", SqlDbType. NVarChar, userName ));
}
Command. ExecuteNonQuery ();
}
- LoadPersonalizationBlobs ()
//
// Summary:
// Load the personalized data from the basic data storage area based on the specified parameters.
//
// Parameters:
// SharedDataBlob:
// It is the data returned from the range of System. Web. UI. WebControls. WebParts. PersonalizationScope. Shared.
//
// WebPartManager:
// Manage System. Web. UI. WebControls. WebParts. WebPartManager for personalized settings.
//
// UserName:
// The username in the System. Web. UI. WebControls. WebParts. PersonalizationScope. User range to use as the keyword to retrieve personalized settings.
//
// UserDataBlob:
// It is the data returned from the range of System. Web. UI. WebControls. WebParts. PersonalizationScope. User.
//
// Path:
// The path of the custom settings to be used as the search keyword in the range of System. Web. UI. WebControls. WebParts. PersonalizationScope. Shared.
protected abstract void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob);
The first few parameters are not described one by one. They andSavePersonalizationBlob ()The parameters used in does not have any difference. Only the personalized parameters are divided into sharedDataBlob and userDataBlob.
If Personalization-InitialScope is set to User in a WebPartManager, the attribute value is stored in userDataBlob; if it is set to Shared, it is stored in sharedDataBlob.
Or set a personalized attribute.
[ResourceWebDisplayName (""), Personalizable (PersonalizationScope. Shared)]
Public long MyProperty
{
Get {
Return _ myProperty ;}
Set {_ myProperty = value ;}
}
For more information, seeHow to read SqlPersonalizationProvider. LoadPersonalizationBlobs
Protected override void LoadPersonalizationBlobs (WebPartManager webPartManager, string path, string userName, ref byte [] sharedDataBlob, ref byte [] userDataBlob)
{
SharedDataBlob = null;
UserDataBlob = null;
SqlConnectionHolder connectionHolder = null;
SqlConnection connection = null;
Try
{
Try
{
ConnectionHolder = this. GetConnectionHolder ();
Connection = connectionHolder. Connection;
This. CheckSchemaVersion (connection );
SharedDataBlob = this. LoadPersonalizationBlob (connection, path, null );
If (! String. IsNullOrEmpty (userName ))
{
UserDataBlob = this. LoadPersonalizationBlob (connection, path, userName );
}
}
Finally
{
If (connectionHolder! = Null)
{
ConnectionHolder. Close ();
ConnectionHolder = null;
}
}
}
Catch
{
Throw;
}
}
2. We have written it.MyPersonalizationProvider,How can we apply it to our system?
You can specify
<WebParts>
<Personalization defaultProvider ="MyPersonalizationProvider">
<Providers>
<Clear/>
<Add name ="MyPersonalizationProvider"Type ="MyCompany.MyPersonalizationProvider"ConnectionStringName =" Default "/>
</Providers>
</Personalization>
</WebParts>
In this case, you can discard the aspnetdbdata database, and create any webpartapplication to initialize the aspnet_regsql.exe
Aspnetdb Database