我基本上是在寻找一些关于构建代码以读取 excel 文件和创建用户帐户的最佳方式的想法。
我已经有了读取 excel 文件和创建帐户的代码,但是我不确定将数据从 excel 表传递到“CreateUser”函数的最佳方法。
任何帮助表示赞赏。
干杯。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
namespace INB201_SAMS.Admin
{
public partial class UploadList : System.Web.UI.Page
{
protected void UploadButton_Click(object sender, EventArgs e)
{
var upload = Path.Combine(Server.MapPath("~/upload"), "myfilename.xlsx");
CSVUpload.SaveAs(upload);
var excelConString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0", upload);
using (OleDbConnection con = new OleDbConnection(excelConString))
{
con.Open();
OleDbCommand com = new OleDbCommand("Select * from [UserUpload$]", con);
OleDbDataReader dr = com.ExecuteReader();
}
File.Delete(upload);
Response.Write("Upload Successfull!");
}
protected bool CreateUser(string UsersUsername, string UsersPassword)
{
try
{
MembershipUser newUser = System.Web.Security.Membership.CreateUser(UsersUsername, UsersPassword);
Roles.AddUserToRole(UsersUsername, "student");
return true;
}
catch (Exception ex)
{
MessageYo.Text = ex.ToString();
return false;
}
}
}
}