任務(wù)2.6 登錄用戶驗證功能設(shè)計

一.GIF效果圖

登錄效果圖.gif

二.畫面主要功能

1.程序獲取數(shù)據(jù)庫中一張表的完整結(jié)構(gòu);
2.實現(xiàn)登錄信息到數(shù)據(jù)庫服務(wù)器上驗證。

三.支持這些功能的后臺數(shù)據(jù)庫表結(jié)構(gòu)

ADMIN.PNG
USERS.PNG

四.ADO.NET查詢數(shù)據(jù)庫的流程

查詢數(shù)據(jù)庫流程.PNG
具體步驟:
  1. 導入命名空間;
  2. 運用Connection對象建立與數(shù)據(jù)庫連接;
  3. 打開連接;
  4. 利用Command對象的ExecuteReader()方法執(zhí)行Select查詢語句;
  5. 利用ExecuteReader()方法返回的DataReader對象讀取數(shù)據(jù),顯示到界面上;
  6. 關(guān)閉連接。

五.重要代碼片段及詳細描述

1.編程訪問數(shù)據(jù)庫

(1).連接數(shù)據(jù)庫

String connStr = "Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True";
SqlConnection sqlConn = new SqlConnection(connStr);

try
{
    // 連接數(shù)據(jù)庫
    sqlConn.Open();

    // 在數(shù)據(jù)庫中查詢USERS表
    
    // 解析數(shù)據(jù)
}
catch (Exception exp)
{
    MessageBox.Show(“訪問數(shù)據(jù)庫出錯");
}
finally
{
    sqlConn.Close();
}


(2).構(gòu)造查詢語句并提交查詢

// 在數(shù)據(jù)庫中查詢USERS表
String sqlStr = "select * from USERS";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
SqlDataReader dr = cmd.ExecuteReader();

(3).獲取數(shù)據(jù)庫返回的數(shù)據(jù)

// 解析數(shù)據(jù)
while (dr.Read())
{
    String Id = dr["ID"].ToString();
    String Name = dr["NAME"].ToString();
    String Password = dr["PASSWORD"].ToString();
    String Phone = dr["PHONE"].ToString();

    // 注意是累加
    this.tb_Users.Text += Id + ", " + Name + ", " + Password + ", " + Phone + "\r\n";
}

2.登錄界面連接數(shù)據(jù)庫

(1).連接數(shù)據(jù)庫

String connStr = "Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True";

SqlConnection sqlConn = new SqlConnection(connStr);
try
{
    sqlConn.Open();

    // 數(shù)據(jù)獲取過程
}
catch (Exception exp)
{
    MessageBox.Show("數(shù)據(jù)庫連接失敗");
}
finally
{
    sqlConn.Close();
}

(2).構(gòu)造查詢語句并提交查詢

String sqlStr = "";
if (this.cbb_UserType.Text == "收銀員")
{
    // 注意USER是SQL Server關(guān)鍵字,表名不能命名為USER,而應(yī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();

(3).獲取數(shù)據(jù)庫返回的數(shù)據(jù)

// 如果從數(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();
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容