亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#中實(shí)現(xiàn)登錄功能的完整步驟

 更新時(shí)間:2021年06月07日 12:06:32   作者:優(yōu)秀是不可能的  
這篇文章主要給大家介紹了關(guān)于C#中實(shí)現(xiàn)登錄功能的相關(guān)資料,我們?cè)谑褂肅#做項(xiàng)目的時(shí)候,基本上都需要制作登錄界面,需要的朋友可以參考下

1. 準(zhǔn)備工作

新建一個(gè)數(shù)據(jù)庫(kù)StudentDB

-- 使用master 數(shù)據(jù)庫(kù)
use master
go
if exists(select *from sysdatabases where name='StudentDB')
drop database StudentDB
go
create database StudentDB
go

在StudentDB中新建三張表

use StudentDB
go

-- 新建學(xué)生表
if exists (select *from sysobjects where name='Student')
drop table Student
go
create table Student
(
stuId   int primary key identity(2000,1),
stuName varchar(30) not null,
stuSex  char(2) not null,
stuAge  int not null,
stuTel  varchar(11) not null,
stuPWd  varchar(30) not null
)

-- 添加學(xué)生表的數(shù)據(jù)
insert into Student (stuName,stuSex,stuAge,stuTel,stuPWd)values('張三','男',21,'12345678543','123456')
insert into Student (stuName,stuSex,stuAge,stuTel,stuPWd)values('趙六','男',21,'12345678543','123456')
insert into Student (stuName,stuSex,stuAge,stuTel,stuPWd)values('韓菲','女',20,'12345678543','123456')


 -- 新建教師表
if exists (select *from sysobjects where name='Teacher')
drop table Teacher
go
create table Teacher
(
tId   int primary key identity(4000,1),
tName varchar(30) not null,
tSex  char(2) not null,
tAge  int not null,
tTel  varchar(11) not null,
tTitle varchar(20),
tPwd   varchar(30) not null    
)
--添加教師表
insert into Teacher(tName,tSex,tAge,tTel,tTitle,tPwd)values('xx','男',32,'12345678901','副教授','123456')

 -- 新建管理員
if exists (select *from sysobjects where name='Admin')
drop table Admin
go
create table Admin
(
adminId   int primary key identity(4000,1),
adminName varchar(30) not null,
adminPWd  varchar(30) not null
)
-- 添加管理員表
insert into Admin(adminName,adminPwd) values('admin','123456')


新建一個(gè)winform 項(xiàng)目,修改文本框name 為,txtUserName,txtPwd;登錄按鈕name 為btnLogin、btnExit。

2.實(shí)現(xiàn)登錄

功能實(shí)現(xiàn)分析

  1. 當(dāng)用戶點(diǎn)擊登錄的時(shí)候,程序首先判斷用戶名、密碼是否為空,然后再根據(jù)單選按鈕的值,去判斷是哪一個(gè)角色進(jìn)行登錄。
  2. 上面的事情做好以后,我們要去把用戶名和密碼拿到數(shù)據(jù)庫(kù)進(jìn)行比較。先使用用戶名當(dāng)作查詢條件,返回一個(gè)用戶對(duì)象(管理員、學(xué)生、教師,根據(jù)具體情況而定,因?yàn)槲覀兪怯弥麈I當(dāng)作用戶名,主鍵可以區(qū)分一個(gè)用戶,所以使用用戶名查詢只返回一條數(shù)據(jù))。判斷對(duì)象是否為null,如果為null則說(shuō)明用戶不存在。否則就判斷密碼是否正確。

準(zhǔn)備實(shí)體類(lèi)

在項(xiàng)目中新建三個(gè)類(lèi),類(lèi)名和表名一致,字段名和表里面的字段名一致。

添加類(lèi),選中項(xiàng)目->添加->類(lèi)

管理員類(lèi)

 public  class Admin
  {
        public int adminId { get; set; }
        public String adminName { get; set; }
        public String adminPwd { get; set; }
  }

學(xué)生類(lèi)

public class Student
    {
        public int stuId { get; set; }
        public string stuName { get; set; }
        public string stuSex { get; set; }
        public int stuAge { get; set; }
        public string stuTel { get; set; }
        public string stuPwd { get; set; }


    }

教師類(lèi)

 public class Teacher
 {
        public int tId { get; set; }
        public string tName { get; set; }
        public string tSex { get; set; }
        public int tAge { get; set; }
        public string tTel { get; set; }
        public string tTitle { get; set; }
        public string tPWd { get; set; }
 }

準(zhǔn)備DBHelper類(lèi)

public class DbHelper
    {
        /// <summary>
        /// 獲取連接對(duì)象
        /// </summary>
        /// <returns></returns>
        public static SqlConnection GetConnection()
        {

            SqlConnection conn = null;
            try
            {
                //可能發(fā)生錯(cuò)誤的代碼
                if (conn == null)
                {
                    conn = new SqlConnection();
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ToString();
                    conn.Open();
                    conn.Close();
                }
                return conn;
            }
            catch (Exception ex)
            {
                //發(fā)生異常以后要做的事情
                throw ex; // 把問(wèn)題拋出,讓程序員知道那里出了錯(cuò)誤
            }
        }

        /// <summary>
        /// 執(zhí)行增刪改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int GetExcuet(string sql)
        {
            // 1. 獲取連接對(duì)象
            SqlConnection conn = GetConnection();
            try
            {
                // 2.打開(kāi)鏈接
                conn.Open();
                //3.創(chuàng)建SqlCommand對(duì)象,sql語(yǔ)句,連接對(duì)象
                SqlCommand cmd = new SqlCommand(sql, conn);
                // 4.執(zhí)行SQL,并返回受影響的行數(shù)
                return cmd.ExecuteNonQuery(); ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }

        /// <summary>
        /// 返回執(zhí)行查詢的結(jié)果
        /// </summary>
        /// <returns></returns>
        public static DataTable GetDataSet(String sql)
        {
            try
            {
                // 1.獲取鏈接對(duì)象
                SqlConnection conn = GetConnection();
                //2.創(chuàng)建適配器對(duì)象
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                //3.創(chuàng)建DataSet 對(duì)象
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
       
      
    }

在App.config中添加 數(shù)據(jù)庫(kù)連接字符串,在configuration標(biāo)簽下進(jìn)行添加

<connectionStrings>
    <add name="connString" connectionString="Data Source=.;Initial Catalog=StudentDB;Persist Security Info=True;User ID=sa;Password=123456"/>
  </connectionStrings>

實(shí)現(xiàn)點(diǎn)擊事件

當(dāng)用戶點(diǎn)擊時(shí)候我們就去執(zhí)行登錄事件

根據(jù)我們分析,我們首先要判斷用戶和密碼是否正確

           //獲取用戶名和密碼
            string username = txtUserName.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            //當(dāng)用戶名為空的時(shí)候就不往下面執(zhí)行了
            if (username.Equals(""))
            {
                MessageBox.Show("用戶名不能為空");
                return;
            }
            if (pwd.Equals(""))
            {
                MessageBox.Show("密碼不能為空");
                return;
            }

在判斷完所有的公共問(wèn)題以后,接下來(lái)我們就要去判斷是哪一個(gè)用戶進(jìn)行的登錄的,我們可以通過(guò)單選按鈕的checked屬性,進(jìn)行判斷,然后分別去調(diào)用他們進(jìn)行登錄的方法。

           //管理員登錄
            if (radAdmin.Checked)
            {
                AdminLogin(username);
            }
            //學(xué)生登錄
            if (radStudent.Checked)
            {
               StudentLogin();
            }
            //教師登錄
            if (radTeacher.Checked)
            {
                TeacherLogin();
            }

管理員登錄方法實(shí)現(xiàn),根據(jù)管理員的用戶名進(jìn)行查詢,判斷返回表的行數(shù),如果行數(shù)小于1,那么表示改用戶不存在,返回null,否則返回一個(gè)管理員對(duì)象。其他的類(lèi)似

  private Admin AdminLogin(String username)
        {

            string sql = string.Format("select *from Admin where adminId={0}",username);
            DataTable table=  DbHelper.GetDataSet(sql);
            //判斷表的行數(shù),大于等于1表示有數(shù)據(jù),用戶存在,否則返回null
            if (table.Rows.Count < 1) return null;

            //新建一個(gè)admin對(duì)象
            Admin admin = new Admin();
            admin.adminId = Convert.ToInt32(table.Rows[0]["adminId"]);
            admin.adminName = table.Rows[0]["adminName"].ToString();
            admin.adminPwd = table.Rows[0]["adminPwd"].ToString();
            return admin;
        }

學(xué)生的登錄方法

 private Student StudentLogin(string username)
        {
            string sql = string.Format("select *from Student where stuId={0}", username);
            DataTable table = DbHelper.GetDataSet(sql);
            //判斷表的行數(shù),大于等于1表示有數(shù)據(jù),用戶存在,否則返回null
            if (table.Rows.Count < 1) return null;

            /*新建一個(gè)student對(duì)象 ,這里只給了三個(gè)字段進(jìn)行了賦值,
             * 因?yàn)槲覀兊卿浀臅r(shí)候,只用到了id和密碼,
             * 其他時(shí)候根據(jù)需求進(jìn)行賦值
            */
            Student student = new Student();
            student.stuId = Convert.ToInt32(table.Rows[0]["stuId"]);
            student.stuName = table.Rows[0]["stuName"].ToString();
            student.stuPwd = table.Rows[0]["stuPwd"].ToString();
            return student;
        }

教師的登錄方法

  private Teacher TeacherLogin(string username)
        {
            string sql = string.Format("select *from Teacher where tId={0}", username);
            DataTable table = DbHelper.GetDataSet(sql);
            //判斷表的行數(shù),大于等于1表示有數(shù)據(jù),用戶存在,否則返回null
            if (table.Rows.Count < 1) return null;

            /*新建一個(gè)student對(duì)象 ,這里只給了三個(gè)字段進(jìn)行了賦值,
             * 因?yàn)槲覀兊卿浀臅r(shí)候,只用到了id和密碼,
             * 其他時(shí)候根據(jù)需求進(jìn)行賦值
            */
            Teacher teacher = new Teacher();
            teacher.tId = Convert.ToInt32(table.Rows[0]["tId"]);
            teacher.tName = table.Rows[0]["tName"].ToString();
            teacher.tPWd = table.Rows[0]["tPWd"].ToString();
            return teacher;
        }

登錄方法完成以后,我要對(duì)返回來(lái)的結(jié)果進(jìn)行處理。首先判斷對(duì)象是否為null,為null就說(shuō)用戶不存在。反之對(duì)象的密碼進(jìn)行比較,密碼正確就彈出登錄成功,密碼不正確就提示密碼不正確。

 private void btnLogin_Click(object sender, EventArgs e)
   {
       //獲取用戶名和密碼
       string username = txtUserName.Text.Trim();
       string pwd = txtPwd.Text.Trim();
       if (username.Equals(""))
       {
           MessageBox.Show("用戶名不能為空");
           return;
       }
       if (pwd.Equals(""))
       {
           MessageBox.Show("密碼不能為空");
           return;
       }
       //管理員登錄
       if (radAdmin.Checked)
       {
           /*為什么要返回來(lái),因?yàn)橐院筇幚磉壿嬁赡茉诓煌?lèi)里面,
            * 這里只是模擬進(jìn)行分層操作*/
          Admin admin=  AdminLogin(username);
          if (admin == null)
          {
               MessageBox.Show("用戶不存在");
               return;
           }
          if (!admin.adminPwd.Equals(pwd))
          {
               MessageBox.Show("密碼錯(cuò)誤");
               return;
          }
     
       }
       //學(xué)生登錄
       if (radStudent.Checked)
       {
           Student  student= StudentLogin(username);
           if (student == null)
           {
               MessageBox.Show("用戶不存在");
               return;
           }
           if (!student.stuPwd.Equals(pwd))
           {
               MessageBox.Show("密碼錯(cuò)誤");
               return;
           }
       }
       //教師登錄
       if (radTeacher.Checked)
       {
           Teacher teacher=  TeacherLogin(username);

           if (teacher == null)
           {
               MessageBox.Show("用戶不存在");
               return;
           }
           if (!teacher.tPWd.Equals(pwd))
           {
               MessageBox.Show("密碼錯(cuò)誤");
               return;
           }
       }

       MessageBox.Show("登錄成功");
   }

總結(jié)

  1. 在登錄中,我們首先先判斷公共的條件,比如說(shuō)用戶名、密碼為空等情況,巧用return ,讓整個(gè)代碼的邏輯變得簡(jiǎn)單。
  2. 在判斷完公共的條件以后,在針對(duì)每一個(gè)具體的角色進(jìn)行判判斷。
  3. 使用使用用戶名進(jìn)行查詢,判斷返回表的行數(shù),如果行數(shù)小于1那么,用戶不存在,否則實(shí)例一個(gè)對(duì)象,并給對(duì)象賦值
  4. 判斷返回對(duì)象的是否為空,如果為空則用戶不存在,否則用戶存在
  5. 最后對(duì)密碼進(jìn)行判斷,如果密碼正確則執(zhí)行登錄成功的操作,如果密碼不正確則彈出密碼不正確的提示。
  6. 項(xiàng)目下載地址:點(diǎn)擊這里

到此這篇關(guān)于C#中實(shí)現(xiàn)登錄功能的文章就介紹到這了,更多相關(guān)C#登錄功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論