登录界面中的用户身份权限验证
第一、用户身份权限验证方法;
public string myUserCheck(string txt_username,string txt_password)
{
string sqlCmdTxt,userSort;
userSort = "nobody";
sqlCmdTxt = "select UserId,UserPassword,UserSort from [User]";
SqlCommand mySqlCmd = new SqlCommand(sqlCmdTxt,this.myLibrayConnection);
try
{
this.myLibrayConnection.Open();
SqlDataReader mySqlDataReader = mySqlCmd.ExecuteReader();
while(mySqlDataReader.Read())
{
if((mySqlDataReader[0].ToString().Trim() == txt_username) && (mySqlDataReader[1].ToString().Trim() == txt_password))
{
userSort = mySqlDataReader[2].ToString().Trim();
break;
}
}
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
this.myLibrayConnection.Close();
return userSort;
}
说明:1、要用到SqlDataReader对象,并对SqlDataReader对象中的数据进行逐一判断。
2、因为User是系统关键字,所以如果自定义的表名用user的话,就需要加上[ ]表明它是用户定义的表而不是系统关键字;
第二、确定按钮的方法。
private void btnOk_Click(object sender, System.EventArgs e)
{
if(myUserCheck(this.textUserID.Text,this.textUserPassword.Text)=="system")
{
this.Visible=false;
Form mainform=new MainForm(this.textUserID.Text,"system");
mainform.ShowDialog();
this.Close();
}
else if(myUserCheck(this.textUserID.Text,this.textUserPassword.Text)=="user")
{
this.Visible=false;
Form mainform=new MainForm(this.textUserID.Text,"user");
mainform.ShowDialog();
this.Close();
}
else
{
if(MessageBox.Show("输入用户密码有误,是否重新登陆","输入有误",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
{
this.textUserID.Clear();
this.textUserPassword.Clear();
//this.textUserID.();
}
else
{
this.Close();
}
}
}
说明:1、利用“用户身份权限验证方法”方法,从数据库中返回用户的权限字符串,并对其进行比较后,进入相关权限页面。