我有asp.net mvc4項目,其中有學生數據庫,也有舊數據庫的.xls格式,我需要將舊數據庫中的所有值遷移到新的一個sql server數據庫。在我的mvc項目中,我有會員控制器,其中包括註冊新學生的方法。另外我編寫控制檯應用程序解析.xls表,並通過寄存器方法將所有值插入到我的新數據庫中。解析excel .xls數據庫到sql server數據庫
控制檯應用程序:
static void Main(string[] args)
{
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MigrateExelSql\Include\TestDb.xlsx; Extended Properties=Excel 12.0;";
MembershipController mc = new MembershipController();
Student student = new Student();
student.Username = null;
student.Password = "password";
student.Email = null;
student.EntryYear = 2014;
student.PassportNumber = 0;
student.IsApproved = true;
student.PasswordFailuresSinceLastSuccess = 0;
student.IsLockedOut = false;
using(OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using(OleDbDataReader dr = command.ExecuteReader())
{
while(dr.Read())
{
string row1Col0 = dr[0].ToString();
Console.WriteLine(row1Col0);
string row1Col1 = dr[1].ToString();
Console.WriteLine(row1Col1);
Console.WriteLine();
student.Username = row1Col0;
student.Email = row1Col1;
try
{
mc.Register(student);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Console.ReadKey();
}
註冊方法
public static MembershipCreateStatus Register(string Username, string Password, string Email, bool IsApproved, string FirstName, string LastName)
{
MembershipCreateStatus CreateStatus;
System.Web.Security.Membership.CreateUser(Username, Password, Email, null, null, IsApproved, null, out CreateStatus);
if (CreateStatus == MembershipCreateStatus.Success)
{
using (UniversityContext Context = new UniversityContext(ConfigurationManager.ConnectionStrings[0].ConnectionString))
{
Student User = Context.Students.FirstOrDefault(Usr => Usr.Username == Username);
User.FirstName = FirstName;
User.LastName = LastName;
Context.SaveChanges();
}
if (IsApproved)
{
FormsAuthentication.SetAuthCookie(Username, false);
}
}
return CreateStatus;
}
的ActionResult POST
public ActionResult Register(Student student)
{
Register(student.Username, student.Password, student.Email, true, student.FirstName, student.LastName);
return RedirectToAction("Index", "Membership");
}
一切都正常工作,當我嘗試添加從Web應用程序的學生,但是當我嘗試從我的分析數據庫中添加這是得到錯誤的下一行
System.Web.Security.Membership.CreateUser(Username, Password, Email, null, null, IsApproved, null, out CreateStatus);
我
Register
方法
,即invalidAnswer
。我更改爲「123456789」,然後下一個錯誤將爲invalidQuestion
,然後我也從null更改爲「123456789」。在此之後,它告訴我,我的invalidPassword。此外,我在我的web.config
requiresQuestionAndAnswer="false"
中加入了我的會員供應商的一行。
我不知道爲什麼它不起作用。有人有任何想法嗎?
當我使用ado.net而不是註冊方法一切正常,但我無法登錄用戶,我用這種方法創建。 – BorHunter