登陸驗證.gif

登陸驗證.gif
整個的流程如上gif所示。
主要功能介紹
用戶通過用戶登陸界面登陸智慧社區(qū)商超管理系統(tǒng)
后臺數(shù)據(jù)表結(jié)構(gòu)
表1.PNG
表2.PNG
后臺數(shù)據(jù)表如上圖所示
表1表2為庫管員和收銀員的登錄表數(shù)據(jù)
ADO.NET查詢數(shù)據(jù)庫的流程
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關(guān)鍵字,表名不能命名為USER,而應(yīng)當(dāng)用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登錄,而不是用戶名,用戶名可能會重復(fù)
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();
}
}
1)導(dǎo)入命名空間,用connection對象建立與數(shù)據(jù)庫連接;
2)打開連接
3)利用command對象的executrreader()放大執(zhí)行select查詢語句;
4)利用ExecuteReader()方法返回的Datareader對象讀取數(shù)據(jù),顯5)示到界面上。
6)關(guān)閉連接
重要代碼片段及描述

image.png
上圖為連接字符串代碼,對數(shù)據(jù)庫連接字符串,之后與數(shù)據(jù)庫連接成功,把數(shù)據(jù)庫數(shù)據(jù)導(dǎo)入其中。

image.png
上圖為構(gòu)造查詢語句并提交查詢。
// 如果從數(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();
}
}
上圖為登錄系統(tǒng)的關(guān)鍵代碼,與數(shù)據(jù)庫數(shù)據(jù)一一對應(yīng),輸入數(shù)據(jù)成功則登錄系統(tǒng),輸入數(shù)據(jù)失敗則無法登錄。