下面的代码我输入没用注册过的用户名时会执行
display.Text = "该拥护已经存在";
但同时又正确把新的用户名添加到的数据库中,也就是说 if 和 else 中的代码同时执行了,真让人感觉奇怪
//用户注册
public void login_Click(object sender, System.EventArgs e)
{
//通过数据验证
if ( Page.IsValid )
{
string strConnt = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + Server.MapPath("db/sample.mdb");
OleDbConnection myConn = new OleDbConnection(strConnt);
//打开连接
myConn.Open();
string StrSQL;
StrSQL = "Select * from zhuce where username = '"+name.Text+"'";
OleDbCommand myCommand = new OleDbCommand(StrSQL,myConn);
OleDbDataReader reader = myCommand.ExecuteReader();
if( reader.Read())
{
reader.Close();
display.Text = "该拥护已经存在";
}
else
{
reader.Close();
StrSQL = "insert into zhuce values ('"+name.Text+"','"+password.Text+"','"+email.Text+"')";
myCommand = new OleDbCommand(StrSQL,myConn);
myCommand.ExecuteNonQuery();
display.Text = "注册成功";
}
myConn.Close();
}
}
display.Text = "该拥护已经存在";
但同时又正确把新的用户名添加到的数据库中,也就是说 if 和 else 中的代码同时执行了,真让人感觉奇怪
//用户注册
public void login_Click(object sender, System.EventArgs e)
{
//通过数据验证
if ( Page.IsValid )
{
string strConnt = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + Server.MapPath("db/sample.mdb");
OleDbConnection myConn = new OleDbConnection(strConnt);
//打开连接
myConn.Open();
string StrSQL;
StrSQL = "Select * from zhuce where username = '"+name.Text+"'";
OleDbCommand myCommand = new OleDbCommand(StrSQL,myConn);
OleDbDataReader reader = myCommand.ExecuteReader();
if( reader.Read())
{
reader.Close();
display.Text = "该拥护已经存在";
}
else
{
reader.Close();
StrSQL = "insert into zhuce values ('"+name.Text+"','"+password.Text+"','"+email.Text+"')";
myCommand = new OleDbCommand(StrSQL,myConn);
myCommand.ExecuteNonQuery();
display.Text = "注册成功";
}
myConn.Close();
}
}
博客展示了一段用户注册代码,当输入未注册用户名时,出现if和else代码同时执行的异常情况,既提示‘该拥护已经存在’,又将新用户名正确添加到数据库中。代码使用OleDb操作数据库进行注册验证和插入。
375

被折叠的 条评论
为什么被折叠?



