import users to ASP.NET membership from a file

后端 未结 3 1983
清歌不尽
清歌不尽 2021-01-06 20:46

I have a site using ASP.NET membership. I also have an excel file with about 60 user records. How can I import the records into the membership database, without having to

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-06 21:17

    foreach (record in recordset) {
        if (!(Roles.RoleExists(record["Role"])) {
            Roles.CreateRole(record["Role"]);
        }
        
        if (Membership.GetUser(record["Username"]) == null) {
            System.Web.Security.MembershipCreateStatus status;
            Membership.CreateUser(record["Username"], record["Password"], record["Email"], record["RSecurityQuestion"], record["SecurityAnswer"], true, status);
            if (status == System.Web.Security.MembershipCreateStatus.Success) {
                Roles.AddUserToRole(record["Username"], record["Role"]);
            }
        }
    }
    

    I can't recall the code to create a recordset from an Excel file off the top of my head.

    EDIT: Here is a good post on how to read/write excel files with ADO.NET

提交回复
热议问题