Profile 详解之增删改查个性化配置文件

2010-03-02 13:15 by ╭☆涩 轨ら, 110 visits, 网摘, 收藏, 编辑

前面的那一篇博文呢,是介绍了 Profile 的 Provider 和 Profile 这个动态类,

以及一些有关 Profile 的最基础的内容,而在这一篇博文里呢,

还是介绍 Profile 的基础内容,也就是很简单的对单个用户的 Profile 的增删改查操作,

说的增删改查呢,其中的增和改其实是一回事,不过是值之间的覆盖问题,

所以事实上并不存在所谓的改,

其中的改只不过是用 Profile 属性的新值来取代了相对应的属性的旧值而已,

而删呢,其实又分为好多种方法,但都是通过 ProfileManager 来完成的,

其中可以按最近活动时间来删除,也可以按 UserName 来删除等等,

而其中查呢又包括几种方式,

您可以使用动态生成的类 ProfileCommon 来查找单个用户的 Profile 属性值,

也可以使用 ProfileManager 这个大类来完成更为强大的查找功能,

而由于这次的 Demo 只是介绍了对单一用户的查找,

所以使用的是动态生成的 ProfileCommon 来完成的,

不过,ProfileManager 则显得更为强大一些,

这样吧,还是列出 MSDN ProfileManager 来参考一下吧

“折叠”图像方法

DeleteInactiveProfiles
删除上次活动日期和时间出现在指定日期和时间之前的用户配置文件数据。
DeleteProfile
从数据源中删除指定用户名的配置文件。
DeleteProfiles
已重载。 从数据源中删除提供的一系列配置文件的配置文件属性和信息。
FindInactiveProfilesByUserName
已重载。 检索配置文件的配置文件信息,在这些配置文件中,

上次活动日期与指定的日期和时间相同或在其之前,

并且配置文件的用户名与指定的名称匹配。
FindProfilesByUserName
已重载。 检索用户名与指定名称匹配的配置文件的配置文件信息。
GetAllInactiveProfiles
已重载。 检索配置文件的用户配置文件数据,在这些配置文件中,

上次活动日期与指定的日期和时间相同或在其之前。
GetAllProfiles
已重载。 在数据源中检索配置文件的用户配置文件数据。
GetNumberOfInactiveProfiles
获取上次活动日期与指定日期相同或在其之前的配置文件的数目。
GetNumberOfProfiles
获取数据源中的配置文件数量。

其中的说明还是很清楚的,这里也就不多做介绍了,不了解的可以自己查 MSDN 咯

在本次的 Demo 中,由于删除也是对单一用户进行的操作,

所以 Demo 中删除用户使用的是 ProfileManager.DeleteProfile(string userName)

而查找则是使用 ProfileCommon . [ 属性名 ] 来直接访问属性,

对于增和改吗,使用的则是 Profile 类,然后调用其 Profile . Sava()方法即可,

下面就看 Demo 吧(先看增和改)

<%@ Page Language="C#"%>

<script runat="server">    
    protected void btnSava_Click(object sender, EventArgs e)
    {
       //设置或者修改 Profile 的各个值
        Profile.住址 = txtAddress.Text;
        Profile.星座 = txtConstellate.Text;
        if (rdBtnHome.Checked)
        {
            Profile.电话.家庭电话 = txtPhone.Text;
        }
        else
        {
            Profile.电话.移动电话 = txtPhone.Text;
        }
        //添加或者修改 Profile
        Profile.Save();
        lblMsg.Text = "添加或者修改成功!!!";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //设置 Profile 必须先登录(匿名暂时不考虑)
        if (User.Identity.IsAuthenticated == false)
        {
           FormsAuthentication.RedirectToLoginPage();
        }
        lblUserName.Text = User.Identity.Name;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 43%;
            height: 241px;
            border: 1px solid #8000FF;
            font-size: small;
        }
        .style2
        {
            height: 42px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-family: 微软雅黑; font-size: small">
        <table class="style1">
            <tr>
                <td>
                    用户名</td>
                <td>
                    <asp:Label ID="lblUserName" runat="server">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    住址</td>
                <td>
                    <asp:TextBox ID="txtAddress" runat="server">
                    </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:RadioButton ID="rdBtnHome" runat="server"
                    GroupName="Phone"
                        Text="家庭电话" Checked="True" />
                </td>
                <td rowspan="2">
                    <asp:TextBox ID="txtPhone" runat="server">
                    </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:RadioButton ID="rdBtnMobile" runat="server"
                    GroupName="Phone"
                        Text="移动电话" />
                </td>
            </tr>
            <tr>
                <td>
                    星座</td>
                <td>
                    <asp:TextBox ID="txtConstellate" runat="server">
                    </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center">
                    <asp:Label ID="lblMsg" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center;
                    font-size: small;" class="style2">
                    <asp:Button ID="btnSava" runat="server"
                    Text="添 加 和 修 改 Profile"
                        οnclick="btnSava_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

然后就来看一看增和改的效果吧

以 ChenJing 登录

image

这样便会在数据库 aspnet_profile 表中存在这条数据了,

等下在查的功能下可以看到

现在就来看一看查和删了

<%@ Page Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //将 Membership 资格成员全部绑定到 DropDownList 上面
            ddlUserName.Items.Clear();
            //获取所有成员
            MembershipUserCollection userCollection =
                Membership.GetAllUsers();

            //遍历这个成员集合
            foreach (MembershipUser user in userCollection)
            {
                //将 UserName 添加到 DropDownList 上面去
                ddlUserName.Items.Add(
                    new ListItem(user.UserName, user.UserName));
            }
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        //删除指定 UserName 的用户的配置文件
        ProfileManager.DeleteProfile(ddlUserName.SelectedValue);
        lblDeleteMsg.Text = "删除 Profile 成功!!!";
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
       
//首先是要实例化一个动态生成的类 ProfileCommon
        //这个类代表了一个用户的 Profile

       
ProfileCommon userCommon =
            Profile.GetProfile(ddlUserName.SelectedValue);

        lblUser.Text =
userCommon.UserName;
        lblAddress.Text =
userCommon.住址;
        lblConstellate.Text = userCommon.星座;
        lblPhone.Text =
userCommon.电话.家庭电话 +
            "  " + userCommon.电话.移动电话;       
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 38%;
            border: 1px solid #8000FF;
            height: 179px;
        }
        .style2
        {
            width: 259px;
        }
        .style3
        {
            width: 117px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-family: 微软雅黑; font-size: small">
        选择要删除 Profile 的用户名<br />
        <br />
        <asp:DropDownList ID="ddlUserName"
        runat="server" Width="200px">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="lblDeleteMsg" runat="server">
        </asp:Label>
        <br />
        <br />
        <asp:Button ID="btnDelete" runat="server"
        OnClick="btnDelete_Click" Text="确 认 删 除" />
    &nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnQuery" runat="server"
       οnclick="btnQuery_Click"
            Text="查 找 Profile" />
        <br />
        <br />
        <table class="style1">
            <tr>
                <td class="style3">
                    用户名</td>
                <td class="style2">
                    <asp:Label ID="lblUser" runat="server">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    住址</td>
                <td class="style2">
                    <asp:Label ID="lblAddress" runat="server">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    电话</td>
                <td class="style2">
                    <asp:Label ID="lblPhone" runat="server">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    星座</td>
                <td class="style2">
                    <asp:Label ID="lblConstellate" runat="server">
                    </asp:Label>
                </td>
            </tr>
            </table>
        <br />
    </div>
    </form>
</body>
</html>

上面的部分便完成了 Profile 针对个人的删和查了

还是看效果比较好

先查找 XiaoZhen 吧

image

然后再查找 ChenJing 吧

image

再来查找一个还没有给其定义 Profile 的试一试(可以看到什么都没有)

image

然后就是演示删除功能了

先删除 ChenJing 吧(可以看到删除后其 Profile 属性值都为空了)

image

再来删除一个本身就没有设置 Profile 的试试看(会把 Profile 当作空来删除)

image

通过上面的这几个 Demo 呢,就完成了针对个人的 Profile 的增删改查,

当然咯,网站的用户成千上万,要是针对个人来删除的话,

那还不搞死人了,将来如果匿名用户太多,

那么隔一段时间就应该把超过多长时间没有活动了的匿名用户的 Profile 进行删除,

这样才能够有效的管理好数据库,这一些内容的话,

后面可能会提到,如果没有提到的话,我想大家可以自己去查 MSDN 来进行解决!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值