任務2.5登錄用戶驗證功能設計
效果圖
主要功能
在界面上輸入用戶名库车、密碼然后點擊登錄巨柒,彈出消息框提示“登錄成功”,打開主界面柠衍,后臺支持數(shù)據(jù)表有收銀員表 庫管員表詳細表結構請查看任務2.4系統(tǒng)數(shù)據(jù)庫設計(http://www.reibang.com/p/7de4bbaa7c0a)
ADO.NET查詢數(shù)據(jù)庫的流程
重要代碼
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構造命令發(fā)送給數(shù)據(jù)庫
String sqlStr = "select * from EMPLOYEE where ID=@id and PASSWORD=@pwd";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 注意是用用戶ID登錄洋满,而不是用戶名,用戶名可能會重復
cmd.Parameters.Add(new SqlParameter("@id", this.tb_User.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@pwd", this.tb_Password.Text.Trim()));
SqlDataReader dr = cmd.ExecuteReader();
// 如果從數(shù)據(jù)庫中查詢到記錄珍坊,則表示可以登錄
if (dr.HasRows)
{
dr.Read();
UserInfo.userId = int.Parse(dr["ID"].ToString());
UserInfo.userName = dr["NAME"].ToString();
UserInfo.userPwd = dr["PASSWORD"].ToString();
UserInfo.userType = dr["TYPE"].ToString();
UserInfo.userPhone = dr["PHONE"].ToString();
MessageBox.Show(UserInfo.userType + "登錄成功");
if (UserInfo.userType == "收銀員")
{
// 顯示收銀員主界面
MainFormUser formUser = new MainFormUser();
formUser.Show();
// 隱藏登錄界面
this.Hide();
}
if (UserInfo.userType == "庫管員")
{
// 顯示庫管員主界面
MainFormAdmin formAdmin = new MainFormAdmin();
formAdmin.Show();
// 隱藏登錄界面
this.Hide();
}
}
else
{
MessageBox.Show("用戶名或密碼錯誤", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}