您的位置:首页 > 编程语言 > ASP

Asp.net如何使用access数据库做profile实现按照区域显示相应语言功能

2012-12-05 17:07 1046 查看
asp.net中按照cultureinfo自动选择相应网页语言的功能,默认会使用sql express。如何才能换成access 2003而不用sql express呢?以下步骤完成之后就可以了。(实例参见http://www.hulilab.com)

1. 在App_Data文件夹下创建一个profile.mdb文件。

其中创建两个表Profiles和ProfileData,字段如下:











2. 在App_Code文件夹中增加一个继承ProfileProvider的类AccessProfileProvider,类的代码见文章后面的附1

3. 编译该类

csc /out:AccessProfileProvider.dll /t:library AccessProfileProvider.cs /r:System.Web.dll /r:System.Configuration.dll

后在App_Code文件夹中会生成一个AccessProfileProvider.dll

4. 在web.config的configuration段中加入

<connectionStrings>

<add name="profileAccessDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}/App_Data/profile.mdb"/>

</connectionStrings>

5. 在web.config的configuration的system.web段中加入

<anonymousIdentification enabled="true"/>

<profile defaultProvider="AccessProfileProvider">

<providers>

<add name="AccessProfileProvider"

type="AccessProfileProvider"

connectionStringName="profileAccessDBConnectionString"

description="Stores and retrieves profile data from an access database."/>

</providers>

<properties>

<add name="myculture" defaultValue="en-us" allowAnonymous ="true"/>

</properties>

</profile>

附1. 继承ProfileProvider的类AccessProfileProvider代码

using System.Web.Profile;

using System.Configuration.Provider;

using System.Collections.Specialized;

using System;

using System.Data;

using System.Data.OleDb;

using System.Configuration;

using System.Diagnostics;

using System.Web;

using System.Collections;

/// <summary>

///AccessProfileProvider 的摘要说明

/// </summary>

public class AccessProfileProvider : ProfileProvider

{

/*

This provider works with the following schema for the table of user data.

CREATE TABLE Profiles

(

UniqueID AutoIncrement NOT NULL PRIMARY KEY,

Username Text (255) NOT NULL,

ApplicationName Text (255) NOT NULL,

IsAnonymous YesNo,

LastActivityDate DateTime,

LastUpdatedDate DateTime,

CONSTRAINT PKProfiles UNIQUE (Username, ApplicationName)

)

CREATE TABLE ProfileData

(

UniqueID Integer,

MyCulture Text (10),

CONSTRAINT FKProfiles2 FOREIGN KEY (UniqueID)

REFERENCES Profiles

)

*/

public AccessProfileProvider()

{

//

//TODO: 在此处添加构造函数逻辑

//

}

//

// Global connection string, generic exception message, event log info.

//

private string eventSource = "AccessProfileProvider";

private string eventLog = "Application";

private string exceptionMessage = "An exception occurred. Please check the event log.";

private string connectionString;

//

// If false, exceptions are thrown to the caller. If true,

// exceptions are written to the event log.

//

private bool pWriteExceptionsToEventLog;

public bool WriteExceptionsToEventLog

{

get { return pWriteExceptionsToEventLog; }

set { pWriteExceptionsToEventLog = value; }

}

//

// System.Configuration.Provider.ProviderBase.Initialize Method

//

public override void Initialize(string name, NameValueCollection config)

{

//

// Initialize values from web.config.

//

if (config == null)

throw new ArgumentNullException("config");

if (name == null || name.Length == 0)

name = "AccessProfileProvider";

if (String.IsNullOrEmpty(config["description"]))

{

config.Remove("description");

config.Add("description", "Access Profile provider");

}

// Initialize the abstract base class.

base.Initialize(name, config);

if (config["applicationName"] == null || config["applicationName"].Trim() == "")

{

pApplicationName = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;

}

else

{

pApplicationName = config["applicationName"];

}

//

// Initialize connection string.

//

ConnectionStringSettings pConnectionStringSettings = ConfigurationManager.

ConnectionStrings[config["connectionStringName"]];

if (pConnectionStringSettings == null ||

pConnectionStringSettings.ConnectionString.Trim() == "")

{

throw new ProviderException("Connection string cannot be blank.");

}

connectionString = string.Format(pConnectionStringSettings.ConnectionString, System.Web.HttpContext.Current.Server.MapPath("~"));

}

//

// System.Configuration.SettingsProvider.ApplicationName

//

private string pApplicationName;

public override string ApplicationName

{

get { return pApplicationName; }

set { pApplicationName = value; }

}

//

// System.Configuration.SettingsProvider methods.

//

//

// SettingsProvider.GetPropertyValues

//

public override SettingsPropertyValueCollection

GetPropertyValues(SettingsContext context,

SettingsPropertyCollection ppc)

{

string username = (string)context["UserName"];

bool isAuthenticated = (bool)context["IsAuthenticated"];

// The serializeAs attribute is ignored in this provider implementation.

SettingsPropertyValueCollection svc =

new SettingsPropertyValueCollection();

foreach (SettingsProperty prop in ppc)

{

SettingsPropertyValue pv = new SettingsPropertyValue(prop);

switch (prop.Name)

{

case "myculture":

pv.PropertyValue = GetMyCulture(username, isAuthenticated);

break;

default:

throw new ProviderException("Unsupported property.");

}

svc.Add(pv);

}

UpdateActivityDates(username, isAuthenticated, true);

return svc;

}

//

// SettingsProvider.SetPropertyValues

//

public override void SetPropertyValues(SettingsContext context,

SettingsPropertyValueCollection ppvc)

{

// The serializeAs attribute is ignored in this provider implementation.

string username = (string)context["UserName"];

bool isAuthenticated = (bool)context["IsAuthenticated"];

int uniqueID = GetUniqueID(username, isAuthenticated, false);

if (uniqueID == 0)

uniqueID = CreateProfileForUser(username, isAuthenticated);

foreach (SettingsPropertyValue pv in ppvc)

{

switch (pv.Property.Name)

{

case "myculture":

SetMyCulture(uniqueID, (string)pv.PropertyValue);

break;

default:

throw new ProviderException("Unsupported property.");

}

}

UpdateActivityDates(username, isAuthenticated, false);

}

//

// UpdateActivityDates

// Updates the LastActivityDate and LastUpdatedDate values

// when profile properties are accessed by the

// GetPropertyValues and SetPropertyValues methods.

// Passing true as the activityOnly parameter will update

// only the LastActivityDate.

//

private void UpdateActivityDates(string username, bool isAuthenticated, bool activityOnly)

{

DateTime activityDate = DateTime.Now;

OleDbConnection conn = new OleDbConnection(connectionString);

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = conn;

if (activityOnly)

{

cmd.CommandText = "UPDATE Profiles Set LastActivityDate = ? " +

"WHERE Username = ? AND ApplicationName = ? AND IsAnonymous = ?";

cmd.Parameters.Add("@LastActivityDate", OleDbType.DBTimeStamp).Value = activityDate.ToString();

cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = username.ToString();

cmd.Parameters.Add("@ApplicationName", OleDbType.VarChar, 255).Value = ApplicationName;

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = !isAuthenticated;

}

else

{

cmd.CommandText = "UPDATE Profiles Set LastActivityDate = ?, LastUpdatedDate = ? " +

"WHERE Username = ? AND ApplicationName = ? AND IsAnonymous = ?";

cmd.Parameters.Add("@LastActivityDate", OleDbType.DBTimeStamp).Value = activityDate.ToString();

cmd.Parameters.Add("@LastUpdatedDate", OleDbType.DBTimeStamp).Value = activityDate.ToString();

cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = username;

cmd.Parameters.Add("@ApplicationName", OleDbType.VarChar, 255).Value = ApplicationName;

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = !isAuthenticated;

}

try

{

conn.Open();

cmd.ExecuteNonQuery();

}

catch (OleDbException e)

{

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "UpdateActivityDates");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

conn.Close();

}

}

//

// GetMyCulture

// Retrieves MyCulture from the database during the call to GetPropertyValues.

//

private string GetMyCulture(string username, bool isAuthenticated)

{

string myculture = "";

OleDbConnection conn = new OleDbConnection(connectionString);

OleDbCommand cmd = new OleDbCommand("SELECT MyCulture FROM Profiles " +

"INNER JOIN ProfileData ON Profiles.UniqueID = ProfileData.UniqueID " +

"WHERE Username = ? AND ApplicationName = ? And IsAnonymous = ?", conn);

cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = username;

cmd.Parameters.Add("@ApplicationName", OleDbType.VarChar, 255).Value = ApplicationName;

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = !isAuthenticated;

try

{

conn.Open();

myculture = (string)cmd.ExecuteScalar();

}

catch (OleDbException e)

{

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "GetMyCulture");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

conn.Close();

}

return myculture;

}

//

// SetMyCulture

// Inserts MyCulture values into the database during

// the call to SetPropertyValues.

//

private void SetMyCulture(int uniqueID, string culturename)

{

if (culturename == null) { culturename = String.Empty; }

OleDbConnection conn = new OleDbConnection(connectionString);

OleDbCommand cmd = new OleDbCommand("DELETE FROM ProfileData WHERE UniqueID = ?", conn);

cmd.Parameters.Add("@UniqueID", OleDbType.Integer).Value = uniqueID;

OleDbCommand cmd2 = new OleDbCommand("INSERT INTO ProfileData (UniqueID, MyCulture) " +

"Values(?, ?)", conn);

cmd2.Parameters.Add("@UniqueID", OleDbType.Integer).Value = uniqueID;

cmd2.Parameters.Add("@MyCulture", OleDbType.VarChar, 10).Value = culturename;

OleDbTransaction tran = null;

try

{

conn.Open();

tran = conn.BeginTransaction();

cmd.Transaction = tran;

cmd2.Transaction = tran;

// Delete any existing values.

cmd.ExecuteNonQuery();

cmd2.ExecuteNonQuery();

tran.Commit();

}

catch (OleDbException e)

{

try

{

tran.Rollback();

}

catch

{

}

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "SetMyCulture");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

conn.Close();

}

}

//

// GetUniqueID

// Retrieves the uniqueID from the database for the current user and application.

//

private int GetUniqueID(string username, bool isAuthenticated, bool ignoreAuthenticationType)

{

OleDbConnection conn = new OleDbConnection(connectionString);

OleDbCommand cmd = new OleDbCommand("SELECT UniqueID FROM Profiles " +

"WHERE Username = ? AND ApplicationName = ?", conn);

cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = username;

cmd.Parameters.Add("@ApplicationName", OleDbType.VarChar, 255).Value = ApplicationName;

if (!ignoreAuthenticationType)

{

cmd.CommandText += " AND IsAnonymous = ?";

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = !isAuthenticated;

}

int uniqueID = 0;

OleDbDataReader reader = null;

try

{

conn.Open();

reader = cmd.ExecuteReader(CommandBehavior.SingleRow);

if (reader.HasRows)

{

reader.Read();

uniqueID = reader.GetInt32(0);

}

}

catch (OleDbException e)

{

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "GetUniqueID");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

if (reader != null) { reader.Close(); }

conn.Close();

}

return uniqueID;

}

//

// CreateProfileForUser

// If no user currently exists in the database,

// a user record is created during

// the call to the GetUniqueID private method.

//

private int CreateProfileForUser(string username, bool isAuthenticated)

{

// Check for valid user name.

if (username == null)

throw new ArgumentNullException("User name cannot be null.");

if (username.Length > 255)

throw new ArgumentException("User name exceeds 255 characters.");

if (username.Contains(","))

throw new ArgumentException("User name cannot contain a comma (,).");

OleDbConnection conn = new OleDbConnection(connectionString);

OleDbCommand cmd = new OleDbCommand("INSERT INTO Profiles (Username, " +

"ApplicationName, LastActivityDate, LastUpdatedDate, " +

"IsAnonymous) Values(?, ?, ?, ?, ?)", conn);

//"IsAnonymous) Values('sd', 'd', '','', 0)", conn);

cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = username;

cmd.Parameters.Add("@ApplicationName", OleDbType.VarChar, 255).Value = ApplicationName;

cmd.Parameters.Add("@LastActivityDate", OleDbType.DBTimeStamp).Value = DateTime.Now.ToString();

cmd.Parameters.Add("@LastUpdatedDate", OleDbType.DBTimeStamp).Value = DateTime.Now.ToString();

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = !isAuthenticated;

OleDbCommand cmd2 = new OleDbCommand("SELECT @@IDENTITY", conn);

int uniqueID = 0;

try

{

conn.Open();

cmd.ExecuteNonQuery();

uniqueID = (int)cmd2.ExecuteScalar();

}

catch (OleDbException e)

{

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "CreateProfileForUser");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

conn.Close();

}

return uniqueID;

}

//

// ProfileProvider.DeleteProfiles(ProfileInfoCollection)

//

public override int DeleteProfiles(ProfileInfoCollection profiles)

{

int deleteCount = 0;

OleDbConnection conn = new OleDbConnection(connectionString);

OleDbTransaction tran = null;

try

{

conn.Open();

tran = conn.BeginTransaction();

foreach (ProfileInfo p in profiles)

{

if (DeleteProfile(p.UserName, conn, tran))

deleteCount++;

}

tran.Commit();

}

catch (Exception e)

{

try

{

tran.Rollback();

}

catch

{

}

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "DeleteProfiles(ProfileInfoCollection)");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

conn.Close();

}

return deleteCount;

}

//

// ProfileProvider.DeleteProfiles(string[])

//

public override int DeleteProfiles(string[] usernames)

{

int deleteCount = 0;

OleDbConnection conn = new OleDbConnection(connectionString);

OleDbTransaction tran = null;

try

{

conn.Open();

tran = conn.BeginTransaction();

foreach (string user in usernames)

{

if (DeleteProfile(user, conn, tran))

deleteCount++;

}

tran.Commit();

}

catch (Exception e)

{

try

{

tran.Rollback();

}

catch

{

}

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "DeleteProfiles(String())");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

conn.Close();

}

return deleteCount;

}

//

// ProfileProvider.DeleteInactiveProfiles

//

public override int DeleteInactiveProfiles(

ProfileAuthenticationOption authenticationOption,

DateTime userInactiveSinceDate)

{

OleDbConnection conn = new OleDbConnection(connectionString);

OleDbCommand cmd = new OleDbCommand("SELECT Username FROM Profiles " +

"WHERE ApplicationName = ? AND " +

" LastActivityDate <= ?", conn);

cmd.Parameters.Add("@ApplicationName", OleDbType.VarChar, 255).Value = ApplicationName;

cmd.Parameters.Add("@LastActivityDate", OleDbType.DBTimeStamp).Value = userInactiveSinceDate;

switch (authenticationOption)

{

case ProfileAuthenticationOption.Anonymous:

cmd.CommandText += " AND IsAnonymous = ?";

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = true;

break;

case ProfileAuthenticationOption.Authenticated:

cmd.CommandText += " AND IsAnonymous = ?";

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = false;

break;

default:

break;

}

OleDbDataReader reader = null;

string usernames = "";

try

{

conn.Open();

reader = cmd.ExecuteReader();

while (reader.Read())

{

usernames += reader.GetString(0) + ",";

}

}

catch (OleDbException e)

{

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "DeleteInactiveProfiles");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

if (reader != null) { reader.Close(); }

conn.Close();

}

if (usernames.Length > 0)

{

// Remove trailing comma.

usernames = usernames.Substring(0, usernames.Length - 1);

}

// Delete profiles.

return DeleteProfiles(usernames.Split(','));

}

//

// DeleteProfile

// Deletes profile data from the database for the

// specified user name.

//

private bool DeleteProfile(string username, OleDbConnection conn, OleDbTransaction tran)

{

// Check for valid user name.

if (username == null)

throw new ArgumentNullException("User name cannot be null.");

if (username.Length > 255)

throw new ArgumentException("User name exceeds 255 characters.");

if (username.Contains(","))

throw new ArgumentException("User name cannot contain a comma (,).");

int uniqueID = GetUniqueID(username, false, true);

OleDbCommand cmd1 = new OleDbCommand("DELETE * FROM ProfileData WHERE UniqueID = ?", conn);

cmd1.Parameters.Add("@UniqueID", OleDbType.Integer).Value = uniqueID;

OleDbCommand cmd3 = new OleDbCommand("DELETE * FROM Profiles WHERE UniqueID = ?", conn);

cmd3.Parameters.Add("@UniqueID", OleDbType.Integer).Value = uniqueID;

cmd1.Transaction = tran;

cmd3.Transaction = tran;

int numDeleted = 0;

// Exceptions will be caught by the calling method.

numDeleted += cmd1.ExecuteNonQuery();

numDeleted += cmd3.ExecuteNonQuery();

if (numDeleted == 0)

return false;

else

return true;

}

//

// ProfileProvider.FindProfilesByUserName

//

public override ProfileInfoCollection FindProfilesByUserName(

ProfileAuthenticationOption authenticationOption,

string usernameToMatch,

int pageIndex,

int pageSize,

out int totalRecords)

{

CheckParameters(pageIndex, pageSize);

return GetProfileInfo(authenticationOption, usernameToMatch,

null, pageIndex, pageSize, out totalRecords);

}

//

// ProfileProvider.FindInactiveProfilesByUserName

//

public override ProfileInfoCollection FindInactiveProfilesByUserName(

ProfileAuthenticationOption authenticationOption,

string usernameToMatch,

DateTime userInactiveSinceDate,

int pageIndex,

int pageSize,

out int totalRecords)

{

CheckParameters(pageIndex, pageSize);

return GetProfileInfo(authenticationOption, usernameToMatch, userInactiveSinceDate,

pageIndex, pageSize, out totalRecords);

}

//

// ProfileProvider.GetAllProfiles

//

public override ProfileInfoCollection GetAllProfiles(

ProfileAuthenticationOption authenticationOption,

int pageIndex,

int pageSize,

out int totalRecords)

{

CheckParameters(pageIndex, pageSize);

return GetProfileInfo(authenticationOption, null, null,

pageIndex, pageSize, out totalRecords);

}

//

// ProfileProvider.GetAllInactiveProfiles

//

public override ProfileInfoCollection GetAllInactiveProfiles(

ProfileAuthenticationOption authenticationOption,

DateTime userInactiveSinceDate,

int pageIndex,

int pageSize,

out int totalRecords)

{

CheckParameters(pageIndex, pageSize);

return GetProfileInfo(authenticationOption, null, userInactiveSinceDate,

pageIndex, pageSize, out totalRecords);

}

//

// ProfileProvider.GetNumberOfInactiveProfiles

//

public override int GetNumberOfInactiveProfiles(

ProfileAuthenticationOption authenticationOption,

DateTime userInactiveSinceDate)

{

int inactiveProfiles = 0;

ProfileInfoCollection profiles =

GetProfileInfo(authenticationOption, null, userInactiveSinceDate,

0, 0, out inactiveProfiles);

return inactiveProfiles;

}

//

// CheckParameters

// Verifies input parameters for page size and page index.

// Called by GetAllProfiles, GetAllInactiveProfiles,

// FindProfilesByUserName, and FindInactiveProfilesByUserName.

//

private void CheckParameters(int pageIndex, int pageSize)

{

if (pageIndex < 0)

throw new ArgumentException("Page index must 0 or greater.");

if (pageSize < 1)

throw new ArgumentException("Page size must be greater than 0.");

}

//

// GetProfileInfo

// Retrieves a count of profiles and creates a

// ProfileInfoCollection from the profile data in the

// database. Called by GetAllProfiles, GetAllInactiveProfiles,

// FindProfilesByUserName, FindInactiveProfilesByUserName,

// and GetNumberOfInactiveProfiles.

// Specifying a pageIndex of 0 retrieves a count of the results only.

//

private ProfileInfoCollection GetProfileInfo(

ProfileAuthenticationOption authenticationOption,

string usernameToMatch,

object userInactiveSinceDate,

int pageIndex,

int pageSize,

out int totalRecords)

{

OleDbConnection conn = new OleDbConnection(connectionString);

// Command to retrieve the total count.

OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM Profiles WHERE ApplicationName = ? ", conn);

cmd.Parameters.Add("@ApplicationName", OleDbType.VarChar, 255).Value = ApplicationName;

// Command to retrieve the profile data.

OleDbCommand cmd2 = new OleDbCommand("SELECT Username, LastActivityDate, LastUpdatedDate, " +

"IsAnonymous FROM Profiles WHERE ApplicationName = ? ", conn);

cmd2.Parameters.Add("@ApplicationName", OleDbType.VarChar, 255).Value = ApplicationName;

// If searching for a user name to match, add the command text and parameters.

if (usernameToMatch != null)

{

cmd.CommandText += " AND Username LIKE ? ";

cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = usernameToMatch;

cmd2.CommandText += " AND Username LIKE ? ";

cmd2.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = usernameToMatch;

}

// If searching for inactive profiles,

// add the command text and parameters.

if (userInactiveSinceDate != null)

{

cmd.CommandText += " AND LastActivityDate <= ? ";

cmd.Parameters.Add("@LastActivityDate", OleDbType.DBTimeStamp).Value = (DateTime)userInactiveSinceDate;

cmd2.CommandText += " AND LastActivityDate <= ? ";

cmd2.Parameters.Add("@LastActivityDate", OleDbType.DBTimeStamp).Value = (DateTime)userInactiveSinceDate;

}

// If searching for a anonymous or authenticated profiles,

// add the command text and parameters.

switch (authenticationOption)

{

case ProfileAuthenticationOption.Anonymous:

cmd.CommandText += " AND IsAnonymous = ?";

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = true;

cmd2.CommandText += " AND IsAnonymous = ?";

cmd2.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = true;

break;

case ProfileAuthenticationOption.Authenticated:

cmd.CommandText += " AND IsAnonymous = ?";

cmd.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = false;

cmd2.CommandText += " AND IsAnonymous = ?";

cmd2.Parameters.Add("@IsAnonymous", OleDbType.Boolean).Value = false;

break;

default:

break;

}

// Get the data.

OleDbDataReader reader = null;

ProfileInfoCollection profiles = new ProfileInfoCollection();

try

{

conn.Open();

// Get the profile count.

totalRecords = (int)cmd.ExecuteScalar();

// No profiles found.

if (totalRecords <= 0) { return profiles; }

// Count profiles only.

if (pageSize == 0) { return profiles; }

reader = cmd2.ExecuteReader();

int counter = 0;

int startIndex = pageSize * (pageIndex - 1);

int endIndex = startIndex + pageSize - 1;

while (reader.Read())

{

if (counter >= startIndex)

{

ProfileInfo p = GetProfileInfoFromReader(reader);

profiles.Add(p);

}

if (counter >= endIndex)

{

cmd.Cancel();

break;

}

counter++;

}

}

catch (OleDbException e)

{

if (WriteExceptionsToEventLog)

{

WriteToEventLog(e, "GetProfileInfo");

throw new ProviderException(exceptionMessage);

}

else

{

throw e;

}

}

finally

{

if (reader != null) { reader.Close(); }

conn.Close();

}

return profiles;

}

//

// GetProfileInfoFromReader

// Takes the current row from the OleDbDataReader

// and populates a ProfileInfo object from the values.

//

private ProfileInfo GetProfileInfoFromReader(OleDbDataReader reader)

{

string username = reader.GetString(0);

DateTime lastActivityDate = new DateTime();

if (reader.GetValue(1) != DBNull.Value)

lastActivityDate = reader.GetDateTime(1);

DateTime lastUpdatedDate = new DateTime();

if (reader.GetValue(2) != DBNull.Value)

lastUpdatedDate = reader.GetDateTime(2);

bool isAnonymous = reader.GetBoolean(3);

// ProfileInfo.Size not currently implemented.

ProfileInfo p = new ProfileInfo(username,

isAnonymous, lastActivityDate, lastUpdatedDate, 0);

return p;

}

//

// WriteToEventLog

// A helper function that writes exception detail to the event

// log. Exceptions are written to the event log as a security

// measure to prevent private database details from being

// returned to the browser. If a method does not return a

// status or Boolean value indicating whether the action succeeded

// or failed, the caller also throws a generic exception.

//

private void WriteToEventLog(Exception e, string action)

{

EventLog log = new EventLog();

log.Source = eventSource;

log.Log = eventLog;

string message = "An exception occurred while communicating with the data source.\n\n";

message += "Action: " + action + "\n\n";

message += "Exception: " + e.ToString();

log.WriteEntry(message);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐