📄 bookshopprofileprovider.cs
字号:
trans.Commit();
}
catch (Exception e)
{
//出现错误,回滚事务
trans.Rollback();
throw new ApplicationException(e.Message);
}
finally
{
conn.Close();
}
}
else
// 删除购物篮信息
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlDelete, parms1);
}
// 更新配置时间等信息
private static void UpdateActivityDates(string username, bool activityOnly)
{
UpdateActivityDatesProfile(username, activityOnly, applicationName);
}
/// <summary>
/// 更新配置时间和最后更新日期等信息
/// </summary>
/// <param name="userName">当前登录用户名</param>
/// <param name="activityOnly">激活标志</param>
/// <param name="appName">应用程序名称</param>
private static void UpdateActivityDatesProfile(string userName, bool activityOnly, string appName)
{
//获取当天日期
DateTime activityDate = DateTime.Now;
string sqlUpdate;
SqlParameter[] parms;
//激活日期
if (activityOnly)
{
//更新配置表
sqlUpdate = "UPDATE Profiles Set LastActivityDate = @LastActivityDate WHERE Username = @Username AND ApplicationName = @ApplicationName;";
parms = new SqlParameter[]{
new SqlParameter("@LastActivityDate", SqlDbType.DateTime),
new SqlParameter("@Username", SqlDbType.VarChar, 256),
new SqlParameter("@ApplicationName", SqlDbType.VarChar, 256)};
parms[0].Value = activityDate;
parms[1].Value = userName;
parms[2].Value = appName;
}
else
{
//更新日期
sqlUpdate = "UPDATE Profiles Set LastActivityDate = @LastActivityDate, LastUpdateDate = @LastUpdatedDate WHERE Username = @Username AND ApplicationName = @ApplicationName;";
parms = new SqlParameter[]{
new SqlParameter("@LastActivityDate", SqlDbType.DateTime),
new SqlParameter("@LastUpdatedDate", SqlDbType.DateTime),
new SqlParameter("@Username", SqlDbType.VarChar, 256),
new SqlParameter("@ApplicationName", SqlDbType.VarChar, 256)};
parms[0].Value = activityDate;
parms[1].Value = activityDate;
parms[2].Value = userName;
parms[3].Value = appName;
}
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlUpdate, parms);
}
/// <summary>
/// 从数据源中删除配置文件属性和信息。
/// </summary>
/// <param name="profiles">属性ID</param>
/// <returns>删除的记录数</returns>
public override int DeleteProfiles(ProfileInfoCollection profiles)
{
int deleteCount = 0;
//通过遍历进行删除-条件为当前登录用户
foreach (ProfileInfo p in profiles)
if (DeleteProfile(p.UserName))
deleteCount++;
return deleteCount;
}
/// <summary>
/// 通过提供的登录用户名删除其配置信息
/// </summary>
/// <param name="usernames">用户名数组</param>
/// <returns>删除的记录数</returns>
public override int DeleteProfiles(string[] usernames)
{
int deleteCount = 0;
foreach (string user in usernames)
if (DeleteProfile(user))
deleteCount++;
return deleteCount;
}
// 通过登录名删除其配置信息
private static bool DeleteProfile(string username)
{
CheckUserName(username);
return dal.DeleteProfile(username, applicationName);
}
// 检查用户
private static void CheckUserName(string userName)
{
if (string.IsNullOrEmpty(userName) || userName.Length > 256 || userName.IndexOf(",") > 0)
throw new ApplicationException(ERR_INVALID_PARAMETER + " user name.");
}
/// <summary>
/// 删除最后一次活动在指定日期之前发生的配置文件的所有用户配置文件数据。
/// </summary>
public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
{
string[] userArray = new string[0];
dal.GetInactiveProfiles((int)authenticationOption, userInactiveSinceDate, ApplicationName).CopyTo(userArray, 0);
return DeleteProfiles(userArray);
}
/// <summary>
/// 检索用户名与指定用户名相匹配的配置文件的配置文件信息。
/// </summary>
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);
}
/// <summary>
/// 检索最后一次活动在指定日期或指定日期之前发生并且用户名与指定用户名相匹配的配置文件的配置文件信息。
/// </summary>
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);
}
/// <summary>
/// 检索数据源中所有配置文件的用户配置文件数据。
/// </summary>
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);
}
/// <summary>
///从数据源中检索最后一次活动在指定日期或指定日期之前发生的配置文件的用户配置文件数据。
/// </summary>
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);
}
/// <summary>
/// 返回最后一次活动在指定日期或指定日期之前发生的配置文件的数目。
/// </summary>
/// <param name="authenticationOption">已通过验证的属性集合</param>
/// <param name="userInactiveSinceDate">当前用户最后激活的日期</param>
/// <returns>发生的配置文件的数目</returns>
public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
{
int inactiveProfiles = 0;
ProfileInfoCollection profiles = GetProfileInfo(authenticationOption, null, userInactiveSinceDate, 0, 0, out inactiveProfiles);
return inactiveProfiles;
}
//判断页数和当前页码参数
private static void CheckParameters(int pageIndex, int pageSize)
{
if (pageIndex < 1 || pageSize < 1)
throw new ApplicationException(ERR_INVALID_PARAMETER + " page index.");
}
//获取属性信息
private static ProfileInfoCollection GetProfileInfo(ProfileAuthenticationOption authenticationOption, string usernameToMatch, object userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
{
//初始化属性集合
ProfileInfoCollection profiles = new ProfileInfoCollection();
totalRecords = 0;
if (pageSize == 0)
return profiles;
int counter = 0;
int startIndex = pageSize * (pageIndex - 1);
int endIndex = startIndex + pageSize - 1;
DateTime dt = new DateTime(1900, 1, 1);
if (userInactiveSinceDate != null)
dt = (DateTime)userInactiveSinceDate;
foreach (CustomProfileInfo profile in dal.GetProfileInfo((int)authenticationOption, usernameToMatch, dt, applicationName, out totalRecords))
{
if (counter >= startIndex)
{
ProfileInfo p = new ProfileInfo(profile.UserName, profile.IsAnonymous, profile.LastActivityDate, profile.LastUpdatedDate, 0);
profiles.Add(p);
}
if (counter >= endIndex)
{
break;
}
counter++;
}
return profiles;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -