

|項(xiàng)目|
|_|
|2.6|登陸用戶驗(yàn)證功能設(shè)計(jì)

2.1本次界面的主要功能
2
3ADO.NET查詢數(shù)據(jù)庫(kù)的流程
具體步驟:
- 導(dǎo)入命名空間;
- 運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
- 打開(kāi)連接;
- 利用Command對(duì)象的ExecuteReader()方法執(zhí)行Select查詢語(yǔ)句;
- 利用ExecuteReader()方法返回的DataReader對(duì)象讀取數(shù)據(jù),顯示到界面上;
- 關(guān)閉連接。
2.4重要代碼
、、、
String connStr = "Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True";
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
sqlConn.Open();
// 數(shù)據(jù)獲取過(guò)程,參見(jiàn)后面PPT
}
catch (Exception exp)
{
MessageBox.Show("數(shù)據(jù)庫(kù)連接失敗");
}
finally
{
sqlConn.Close();
}
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 + "登錄成功");
// 處理登錄,見(jiàn)右邊代碼段
}
if (UserInfo.userType == "收銀員")
{
// 顯示收銀員主界面
MainFormUser formUser = new MainFormUser();
formUser.Show();
// 隱藏登錄界面
this.Hide();
}
if (UserInfo.userType == "庫(kù)管員")
{
// 顯示庫(kù)管員主界面
MainFormAdmin formAdmin = new MainFormAdmin();
formAdmin.Show();
// 隱藏登錄界面
this.Hide();
}
|2.7|密碼修改界面功能設(shè)計(jì)

3.ADO.NET更新數(shù)據(jù)庫(kù)的流程
具體步驟:
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫(kù)連接字符串,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接;
- 打開(kāi)連接;
- 利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Update語(yǔ)句;
- 通過(guò)ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示;
- 關(guān)閉連接。
、、、
String userName = this.tb_User.Text.Trim();
String newPwd = this.tb_NewPwd.Text.Trim();
String confPwd = this.tb_ConfirmPwd.Text.Trim();
// 驗(yàn)證輸入信息
if (newPwd.Equals(""))
{
MessageBox.Show("請(qǐng)輸入新密碼", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (confPwd.Equals(""))
{
MessageBox.Show("請(qǐng)輸入確認(rèn)密碼", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (newPwd != confPwd)
{
MessageBox.Show("兩次密碼不一致", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 連接字符串,注意與實(shí)際環(huán)境保持一致
String connStr = "Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True";
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open();
// 構(gòu)造UPDATE命令,更改數(shù)據(jù)庫(kù),參見(jiàn)后面PPT
}
catch (Exception exp)
{
MessageBox.Show("訪問(wèn)數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
// 構(gòu)造UPDATE命令
String sqlStr = "update EMPLOYEE set PASSWORD=@pwd where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數(shù)賦值
cmd.Parameters.Add(new SqlParameter("@pwd", newPwd));
cmd.Parameters.Add(new SqlParameter("@id", UserInfo.userId));
// 將命令發(fā)送給數(shù)據(jù)庫(kù)
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("密碼修改成功");
this.Close();
}
else
{
MessageBox.Show("密碼修改錯(cuò)誤");
}
、、、