protected void signup_submit_ServerClick(object sender, ImageClickEventArgs e)
{
//入数据库
string connStr = EightourConfiguration.DbConnectionString;
string loginNameValue = LoginName.Value;
string passwordValue = Password.Value;
string mobileValue = Mobile.Value;
string emailValue = Email.Value;
string sexValue = Sex.SelectedValue;
int sex = 2;
if (sexValue == "T")
{
sex = 1;
}
else if (sexValue == "F")
{
sex = 0;
}
string chineseNameValue = ChineseName.Value;
string addressValue = Address.Value;
string postcodeValue = Postcode.Value;
string telValue = Tel.Value;
//注意参数顺序
string updateSql = "insert into MB_BaseInfo ([LoginName],[Password],[ChineseName],[Sex]) values (@Para_LoginName,@Para_Password,@Para_ChineseName,@Para_Sex)";
OleDbCommand cmd = new OleDbCommand(updateSql);
//注意参数顺序与上面一一对应
OleDbParameter Para_LoginName = new OleDbParameter("@Para_LoginName", System.Data.OleDb.OleDbType.VarChar, 50);
Para_LoginName.Value = loginNameValue;
cmd.Parameters.Add(Para_LoginName);
OleDbParameter Para_Password = new OleDbParameter("@Para_Password", System.Data.OleDb.OleDbType.VarChar, 50);
Para_Password.Value = passwordValue;
cmd.Parameters.Add(Para_Password);
OleDbParameter Para_ChineseName = new OleDbParameter("@Para_ChineseName", System.Data.OleDb.OleDbType.VarChar, 50);
Para_ChineseName.Value = chineseNameValue;
cmd.Parameters.Add(Para_ChineseName);
OleDbParameter Para_Sex = new OleDbParameter("@Para_Sex", System.Data.OleDb.OleDbType.Integer);
Para_Sex.Value = sex;
cmd.Parameters.Add(Para_Sex);
int lastId = -1;
using (OleDbConnection myConnection = new OleDbConnection(connStr))
{
cmd.Connection = myConnection;
myConnection.Open();
OleDbTransaction trans = myConnection.BeginTransaction();
cmd.Transaction = trans;
try
{
cmd.ExecuteNonQuery();
cmd.CommandText = "select @@identity";
lastId = Convert.ToInt32(cmd.ExecuteScalar().ToString());
trans.Commit();
}
catch (Exception exc)
{
trans.Rollback();
throw new Exception(exc.Message);
}
finally
{
}
}
if (lastId == -1) return;
bool isSuccess = false;
//注意参数顺序
updateSql = "insert into MB_PersonalInfo ([BaseInfoId],[Tel],[Mobile],[Address],[Postcode],[Email]) values(@Para_BaseInfoId,@Para_Tel,@Para_Mobile,@Para_Address,@Para_Postcode,@Para_Email)";
cmd = new OleDbCommand(updateSql);
//注意参数顺序与上面并不一一对应
OleDbParameter Para_BaseInfoId = new OleDbParameter("@Para_BaseInfoId", System.Data.OleDb.OleDbType.Integer);
Para_BaseInfoId.Value = lastId;
cmd.Parameters.Add(Para_BaseInfoId);
OleDbParameter Para_Tel = new OleDbParameter("@Para_Tel", System.Data.OleDb.OleDbType.VarChar, 50);
Para_Tel.Value = telValue;
cmd.Parameters.Add(Para_Tel);
//这里现在是Postcode邮政编码,上面第三个参数是Mobile,不对应了。
OleDbParameter Para_Postcode = new OleDbParameter("@Para_Postcode", System.Data.OleDb.OleDbType.VarChar, 50);
Para_Postcode.Value = postcodeValue;
cmd.Parameters.Add(Para_Postcode);
OleDbParameter Para_Mobile = new OleDbParameter("@Para_Mobile", System.Data.OleDb.OleDbType.VarChar, 50);
Para_Mobile.Value = mobileValue;
cmd.Parameters.Add(Para_Mobile);
OleDbParameter Para_Address = new OleDbParameter("@Para_Address", System.Data.OleDb.OleDbType.VarChar,255);
Para_Address.Value = addressValue;
cmd.Parameters.Add(Para_Address);
OleDbParameter Para_Email = new OleDbParameter("@Para_Email", System.Data.OleDb.OleDbType.VarChar, 255);
Para_Email.Value = emailValue;
cmd.Parameters.Add(Para_Email);
using (OleDbConnection myConnection = new OleDbConnection(connStr))
{
cmd.Connection = myConnection;
myConnection.Open();
OleDbTransaction trans = myConnection.BeginTransaction();
cmd.Transaction = trans;
try
{
cmd.ExecuteNonQuery();
trans.Commit();
isSuccess = true;
}
catch (Exception exc)
{
trans.Rollback();
throw new Exception(exc.Message);
}
finally
{
}
}
if (isSuccess)
{
lblNotifier.Visible = true;
lblNotifier.Text = "<br /><font color='red'><b>你已成功注册!</b></font><script language='Javascript'>alert('恭喜您,注册成功!');</script>";
}
else
{
lblNotifier.Visible = false;
}
}
执行上面代码时,结果发现数据库表中Mobile的值成了Postcode的值,Address的值成了Mobile的值.....乱套了。
如果将上面的相关顺序调整一下,变成一一对应,就可以了。
在SQL Server数据库中就不会涉及此类问题。看来,是ACCESS的局限。