import users to ASP.NET membership from a file

后端 未结 3 1963
清歌不尽
清歌不尽 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:13

    Here's three options for you

    1. There may be an import data option in SQL Server Management Studio, you can use this (I just checked in SQL Server Express and it wasn't there but I'm certain it exists in the full version of Management Studio - can't check at the moment).

    2. Use SSIS if you have it

    3. You can do it old school and use Excel to build up a SQL string with references to the cells. Do this for the first row and then copy down. Copy all the statements into a query window and execute!

    0 讨论(0)
  • 2021-01-06 21:15

    Choice 1

    If you're lucky, Excel has a way to export the rows directly to your database (this depends on which Database you're using)

    Choice 2

    Write a program that iterates over each row in the excel table and inserts it manually into your database (again, process depends on which one)

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题