C#中實(shí)現(xiàn)登錄功能的完整步驟
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)分析
- 當(dāng)用戶點(diǎn)擊登錄的時(shí)候,程序首先判斷用戶名、密碼是否為空,然后再根據(jù)單選按鈕的值,去判斷是哪一個(gè)角色進(jìn)行登錄。
- 上面的事情做好以后,我們要去把用戶名和密碼拿到數(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é)
- 在登錄中,我們首先先判斷公共的條件,比如說(shuō)用戶名、密碼為空等情況,巧用return ,讓整個(gè)代碼的邏輯變得簡(jiǎn)單。
- 在判斷完公共的條件以后,在針對(duì)每一個(gè)具體的角色進(jìn)行判判斷。
- 使用使用用戶名進(jìn)行查詢,判斷返回表的行數(shù),如果行數(shù)小于1那么,用戶不存在,否則實(shí)例一個(gè)對(duì)象,并給對(duì)象賦值
- 判斷返回對(duì)象的是否為空,如果為空則用戶不存在,否則用戶存在
- 最后對(duì)密碼進(jìn)行判斷,如果密碼正確則執(zhí)行登錄成功的操作,如果密碼不正確則彈出密碼不正確的提示。
- 項(xiàng)目下載地址:點(diǎn)擊這里
到此這篇關(guān)于C#中實(shí)現(xiàn)登錄功能的文章就介紹到這了,更多相關(guān)C#登錄功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#實(shí)現(xiàn)簡(jiǎn)單的登錄界面
- C# Winform中實(shí)現(xiàn)主窗口打開(kāi)登錄窗口關(guān)閉的方法
- C#有效防止同一賬號(hào)多次登錄(附三種方法)
- C#實(shí)現(xiàn)的三種模擬自動(dòng)登錄和提交POST信息的方法
- C#.NET實(shí)現(xiàn)網(wǎng)頁(yè)自動(dòng)登錄的方法
- C#使用WebClient登錄網(wǎng)站并抓取登錄后的網(wǎng)頁(yè)信息實(shí)現(xiàn)方法
- .NET C#使用微信公眾號(hào)登錄網(wǎng)站
- C#使用HttpWebRequest與HttpWebResponse模擬用戶登錄
- C#可用于登錄驗(yàn)證碼的四位隨機(jī)數(shù)生成方法
- C#實(shí)現(xiàn)登錄窗口(不用隱藏)
相關(guān)文章
WPF實(shí)現(xiàn)上下滾動(dòng)字幕效果
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)上下滾動(dòng)字幕效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10insert語(yǔ)句太長(zhǎng)用StringBuilder優(yōu)化一下
insert語(yǔ)句太長(zhǎng)用StringBuilder優(yōu)化一下,下面是示例代碼,需要的朋友可以研究研究2014-07-07C#?OpenCvSharp?顏色反轉(zhuǎn)實(shí)例詳解
OpenCVSharp是OpenCV的.NET?wrapper,它比Emgucv更接近于原始的OpenCV,并且有很多的樣例參考,其采用LGPL發(fā)行,對(duì)商業(yè)應(yīng)用友好(基本上相當(dāng)于BSD),這篇文章主要介紹了C#?OpenCvSharp?顏色反轉(zhuǎn)的知識(shí),需要的朋友可以參考下2024-02-02C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法
下面小編就為大家?guī)?lái)一篇C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08unity實(shí)現(xiàn)方向盤(pán)轉(zhuǎn)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)方向盤(pán)轉(zhuǎn)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法,涉及WinForm窗口滾動(dòng)字幕設(shè)置的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義
ContextMenu 類(lèi)表示當(dāng)用戶在控件或窗體的特定區(qū)域上單擊鼠標(biāo)右鍵時(shí)會(huì)顯示的快捷菜單,要想實(shí)現(xiàn)自定義的Menu和ContextMenu效果,大家可以通過(guò)派生ProfessionalColorTable類(lèi),下面小編把實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及ContextMenu自定義給大家整理一下2015-08-08