一.效果圖
二.描述主要功能
通過(guò)主界面選擇收銀員或者庫(kù)管員登陸绊谭,在輸入相應(yīng)的賬號(hào)密碼彈出相應(yīng)的界面
三.ADO.NET查詢數(shù)據(jù)庫(kù)的流程
1.連接字符串
2.鏈接數(shù)據(jù)庫(kù)
sqlConn.Open();
3.構(gòu)造命令
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";
}
4.字符串賦值
cmd.Parameters.Add(new SqlParameter("@id", this.tb_User.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@pwd", this.tb_Password.Text.Trim()));
- 將命令發(fā)送給數(shù)據(jù)庫(kù)
SqlDataReader dr = cmd.ExecuteReader();
四.貼入重要代碼片段青柄,并進(jìn)行詳細(xì)描述
1.將收銀員設(shè)置為默認(rèn)登錄類型
2.點(diǎn)擊登錄鍵則登錄系統(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關(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登錄跋理,而不是用戶名,用戶名可能會(huì)重復(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ù)庫(kù)中查詢到記錄筷厘,則表示可以登錄
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 == "庫(kù)管員")
{
// 顯示庫(kù)管員主界面
MainFormAdmin formAdmin = new MainFormAdmin();
formAdmin.Show();
// 隱藏登錄界面
this.Hide();
}
}
else
{
MessageBox.Show("用戶名或密碼錯(cuò)誤", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception exp)
{
MessageBox.Show("數(shù)據(jù)庫(kù)連接失敗");
}
finally
{
sqlConn.Close();
}