2.5登陸用戶驗證功能設計
一障簿、制作效果
創(chuàng)建數(shù)據(jù)庫表盹愚、管理員表(ADMIN)和收銀員表(USERS)。
ADMIN.PNG
USERS.PNG
制作登陸界面站故,用戶類型分為收銀員和庫管員(comboBox控件實現(xiàn))皆怕,連接數(shù)據(jù)庫當用戶類型、用戶名西篓、密碼和數(shù)據(jù)庫的值一致時登陸并提示愈腾!庫管員界面類似。
2.PNG
3.PNG
收銀員登陸界面污淋、制作DIM窗體來制作收銀員界面和庫管員界面顶滩,暫時當點擊注銷和退出時實現(xiàn)功能
4.PNG
5.PNG
二、連接數(shù)據(jù)庫方法(方法一)
連接數(shù)據(jù)庫.gif
步驟解析
1寸爆、視圖——服務器資源管理器——數(shù)據(jù)連接——添加連接——選擇Microsoft Visual Studio 2008——輸入服務器名(.)——選擇數(shù)據(jù)庫表——完成連接(獲得連接字符串)
連接字符串.PNG
三礁鲁、ADM.NET流程
ADM.PNG
四、主要代碼
(一)赁豆、登陸界面
1仅醇、登陸按鈕
// 點擊“登錄”按鈕則登錄系統(tǒng)
private void bt_Login_Click(object sender, EventArgs e)
{
String connStr = "Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True";
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
sqlConn.Open();
String sqlStr = "";
if (this.cbb_UserType.Text == "收銀員")
{
// 注意USER是SQL Server關鍵字,表名不能命名為USER魔种,而應當用USERS
sqlStr = "select * from USERS where ID=@id and PASSWORD=@pwd";
}
else
{
sqlStr = "select * from ADMIN 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.userPhone = dr["PHONE"].ToString();
UserInfo.userType = this.cbb_UserType.Text;
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ù)庫連接失敗");
}
finally
{
sqlConn.Close();
}
}
2叶摄、退出按鈕
// 點擊“退出”按鈕則退出應用程序
private void bt_Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
3、默認用戶類型(收銀員)
// 窗口加載時安拟,設置默認角色為“收銀員”
private void frm_Login_Load(object sender, EventArgs e)
{
// 設置收銀員為默認登錄類型
this.cbb_UserType.SelectedIndex = 0;
// 禁止Tab鍵停留到LinkLabel上
this.ll_Register.TabStop = false;
this.ll_Forget.TabStop = false;
}
(二)蛤吓、收銀員、庫管員界面
1糠赦、注銷
// 注銷當前登錄会傲,回到登錄界面
private void tsmi_Logout_Click(object sender, EventArgs e)
{
if (MessageBox.Show("確認注銷锅棕?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
// 重新啟動程序,以顯示登錄窗口
Application.Restart();
}
}
退出
// 退出系統(tǒng)
private void tsmi_Exit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("確認退出淌山?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
Application.Exit();
}
}