C#中winform窗體實(shí)現(xiàn)注冊(cè)/登錄功能實(shí)例(DBHelper類)
winform窗體應(yīng)用程序,實(shí)現(xiàn)注冊(cè)和登錄功能
1.1.開發(fā)環(huán)境:Visual Studio 2019 + SQL Server 2012 Management Studio
1.2.winform基本窗體界面
registeForm注冊(cè)界面
loginForm登錄界面
1.3.DBHelper類
DBHelper從字面上理解為“數(shù)據(jù)庫(kù)幫助類”,在這里主要是進(jìn)行winform窗體應(yīng)用程序和數(shù)據(jù)庫(kù)的數(shù)據(jù)交互。話不多說(shuō),我直接放在下面了
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //導(dǎo)入命名空間 using System.Data; using System.Data.SqlClient; namespace oneWinForms { class DBHelper { //定義連接字符串 private static string connStr = "server=.;database=oneWinFormDB;uid=sa;pwd=123456"; /// <summary> /// 查詢方法 DataSet /// </summary> /// <param name="sql">查詢sql語(yǔ)句</param> /// <returns>返回DataSet數(shù)據(jù)表格</returns> public static DataSet GetDataSet(string sql) { //創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象 SqlConnection conn = new SqlConnection(connStr); //創(chuàng)建數(shù)據(jù)適配器對(duì)象 SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //創(chuàng)建空數(shù)據(jù)表格對(duì)象 DataSet ds = new DataSet(); sda.Fill(ds); return ds; } /// <summary> /// 執(zhí)行增刪改查語(yǔ)句 /// </summary> /// <param name="sql">增刪改sql語(yǔ)句</param> /// <returns>返回增刪改執(zhí)行結(jié)果</returns> public static bool ExecuteNonQuery(string sql) { SqlConnection conn = new SqlConnection(connStr); //打開數(shù)據(jù)庫(kù)連接 conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); return cmd.ExecuteNonQuery() > 0; } /// <summary> /// 執(zhí)行集合函數(shù)操作 查詢首行首列,返回object /// </summary> /// <param name="sql"></param> /// <param name="par"></param> /// <returns></returns> public static object ExecuteScalar(string sql) { SqlConnection conn = new SqlConnection(connStr);//創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象 conn.Open();//打開數(shù)據(jù)庫(kù)連接 SqlCommand cmd = new SqlCommand(sql, conn); object result = cmd.ExecuteScalar(); conn.Close(); return result; } } }
記得導(dǎo)入一下命名空間
//導(dǎo)入命名空間 using System.Data; using System.Data.SqlClient;
database 后面寫的是數(shù)據(jù)庫(kù)的名字;
uid是SQL server2012 Management Studio的管理員名稱,它默認(rèn)是sa;
pwd后面是SQL server2012 Management Studio的密碼,這里設(shè)置為123456;
1.4.建立數(shù)據(jù)庫(kù)
use master create database oneWinFormDB on ( name='oneWinFormDB', filename='D:\MyDB\oneWinFormDB.MDF' ) go use oneWinFormDB go create table oneTb ( userId int identity(1,1) primary key, uTel varchar(20), uPwd varchar(20) ) go insert into oneTb(uTel,uPwd) values('12345678910','123456'); insert into oneTb(uTel,uPwd) values('12345678911','123456'); insert into oneTb(uTel,uPwd) values('12345678912','123456'); insert into oneTb(uTel,uPwd) values('12345678913','123456'); insert into oneTb(uTel,uPwd) values('12345678914','123456'); insert into oneTb(uTel,uPwd) values('12345678915','123456'); select*from oneTb--查詢表
用sql語(yǔ)句建立數(shù)據(jù)庫(kù),看起來(lái)很簡(jiǎn)單吧。
1.5.主要功能代碼
注冊(cè)按鈕
//注冊(cè)按鈕 private void resBtn_Click(object sender, EventArgs e) { //非空驗(yàn)證 if (tel.Text=="")//非空驗(yàn)證 { MessageBox.Show("手機(jī)號(hào)不為空!"); } if (pwd1.Text == "") { MessageBox.Show("密碼不為空!"); } if (pwd2.Text == "") { MessageBox.Show("請(qǐng)確認(rèn)密碼!"); } if (pwd1.Text != pwd2.Text)//驗(yàn)證密碼一致 { MessageBox.Show("兩次輸入密碼不一致!"); } else { string sql = string.Format(@"insert into oneTb(uTel,uPwd) values('{0}','{1}');",tel.Text, pwd1.Text);//添加的sql語(yǔ)句 if (DBHelper.ExecuteNonQuery(sql))//調(diào)用ExecuteNonQuery方法進(jìn)行增加操作 { MessageBox.Show("用戶注冊(cè)成功,請(qǐng)登錄!");//注冊(cè)成功后提醒 LoginForm lf = new LoginForm(); lf.Show();//注冊(cè)完成后,自動(dòng)跳轉(zhuǎn)到登錄界面 this.Hide();//隱藏該頁(yè)面 } else { MessageBox.Show("注冊(cè)失敗,請(qǐng)重試!");//注冊(cè)失敗后提醒 } } }
手動(dòng)跳轉(zhuǎn)到登錄界面
//登錄按鈕--手動(dòng)跳轉(zhuǎn)到登錄界面 private void loginBtn_Click(object sender, EventArgs e) { LoginForm lf = new LoginForm(); lf.Show();//跳轉(zhuǎn)到登錄界面 }
登錄按鈕
//登錄按鈕 private void LoginBtn_Click(object sender, EventArgs e) { //非空驗(yàn)證 if (tel.Text == "")//非空驗(yàn)證 { MessageBox.Show("手機(jī)號(hào)不為空!"); } if (pwd.Text == "")//非空驗(yàn)證 { MessageBox.Show("密碼不為空!"); } else { string sql = string.Format("select count(*) from oneTb where uTel='{0}'and uPwd='{1}'", tel.Text, pwd.Text); int i = Convert.ToInt32(DBHelper.ExecuteScalar(sql)); ;//調(diào)用查詢單個(gè)值的方法 if (i>0) { MessageBox.Show("登錄成功"); } else { MessageBox.Show("請(qǐng)檢查手機(jī)號(hào)和密碼后重試!","登錄失敗"); } } }
手動(dòng)跳轉(zhuǎn)到注冊(cè)界面
//注冊(cè)按鈕--手動(dòng)跳轉(zhuǎn)到注冊(cè)界面 private void resBtn_Click(object sender, EventArgs e) { registForm rf = new registForm(); rf.Show();//跳轉(zhuǎn)到注冊(cè)界面 }
注冊(cè)界面完整源碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace oneWinForms { public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); } //注冊(cè)按鈕--手動(dòng)跳轉(zhuǎn)到跳轉(zhuǎn)到注冊(cè)界面 private void resBtn_Click(object sender, EventArgs e) { registForm rf = new registForm(); rf.Show();//跳轉(zhuǎn)到注冊(cè)界面 } //登錄按鈕 private void LoginBtn_Click(object sender, EventArgs e) { //非空驗(yàn)證 if (tel.Text == "")//非空驗(yàn)證 { MessageBox.Show("手機(jī)號(hào)不為空!"); } if (pwd.Text == "")//非空驗(yàn)證 { MessageBox.Show("密碼不為空!"); } else { string sql = string.Format("select count(*) from oneTb where uTel='{0}'and uPwd='{1}'", tel.Text, pwd.Text); int i = Convert.ToInt32(DBHelper.ExecuteScalar(sql)); ;//調(diào)用查詢單個(gè)值的方法 if (i>0) { MessageBox.Show("登錄成功"); } else { MessageBox.Show("請(qǐng)檢查手機(jī)號(hào)和密碼后重試!","登錄失敗"); } } } } }
登錄界面完整源碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace oneWinForms { public partial class registForm : Form { public registForm() { InitializeComponent(); } //注冊(cè)按鈕 private void resBtn_Click(object sender, EventArgs e) { //非空驗(yàn)證 if (tel.Text=="")//非空驗(yàn)證 { MessageBox.Show("手機(jī)號(hào)不為空!"); } if (pwd1.Text == "") { MessageBox.Show("密碼不為空!"); } if (pwd2.Text == "") { MessageBox.Show("請(qǐng)確認(rèn)密碼!"); } if (pwd1.Text != pwd2.Text)//驗(yàn)證密碼一致 { MessageBox.Show("兩次輸入密碼不一致!"); } else { string sql = string.Format(@"insert into oneTb(uTel,uPwd) values('{0}','{1}');", tel.Text, pwd1.Text);//添加的sql語(yǔ)句 if (DBHelper.ExecuteNonQuery(sql))//調(diào)用ExecuteNonQuery方法進(jìn)行增加操作 { MessageBox.Show("用戶注冊(cè)成功,請(qǐng)登錄!");//注冊(cè)成功后提醒 LoginForm lf = new LoginForm(); lf.Show();//跳轉(zhuǎn)到登錄界面 this.Hide();//隱藏該頁(yè)面 } else { MessageBox.Show("注冊(cè)失敗,請(qǐng)重試!");//注冊(cè)失敗后提醒 } } } //登錄按鈕--手動(dòng)跳轉(zhuǎn)到登錄界面 private void loginBtn_Click(object sender, EventArgs e) { LoginForm lf = new LoginForm(); lf.Show();//跳轉(zhuǎn)到登錄界面 } } }
驗(yàn)證及注冊(cè)/登錄效果圖
密碼非空驗(yàn)證
手機(jī)號(hào)非空驗(yàn)證
驗(yàn)證密碼一致
注冊(cè)完成后頁(yè)面
登錄成功界面
這個(gè)代碼可能有小bug,還請(qǐng)多多指教。
總結(jié)
到此這篇關(guān)于C#中winform窗體實(shí)現(xiàn)注冊(cè)/登錄功能(DBHelper類)的文章就介紹到這了,更多相關(guān)winform窗體實(shí)現(xiàn)注冊(cè)登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#編程實(shí)現(xiàn)帶有Aero效果的窗體示例
這篇文章主要介紹了C#編程實(shí)現(xiàn)帶有Aero效果的窗體,涉及C#調(diào)用動(dòng)態(tài)鏈接庫(kù)針對(duì)窗體屬性的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法,涉及C#基于Process類操作WinRar命令的相關(guān)實(shí)現(xiàn)技巧,代碼簡(jiǎn)潔實(shí)用,需要的朋友可以參考下2016-06-06C#動(dòng)態(tài)創(chuàng)建Access數(shù)據(jù)庫(kù)及表的方法
這篇文章主要介紹了C#動(dòng)態(tài)創(chuàng)建Access數(shù)據(jù)庫(kù)及表的方法,以實(shí)例形式分析了創(chuàng)建access數(shù)據(jù)庫(kù)及在access數(shù)據(jù)庫(kù)中建表的完整過(guò)程,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12C# KeyUp事件中MessageBox的回車(Enter)鍵回調(diào)問(wèn)題解決方案
這篇文章主要介紹了C# KeyUp事件中MessageBox的回車(Enter)鍵回調(diào)問(wèn)題解決方案,需要的朋友可以參考下2014-07-07c#標(biāo)準(zhǔn)idispose模式使用示例
下面將把C#里實(shí)現(xiàn)IDispose模式的代碼展現(xiàn)出來(lái),大家一起來(lái)學(xué)習(xí)一下,它的使用場(chǎng)合也很多的,當(dāng)我們手動(dòng)對(duì)網(wǎng)站,數(shù)據(jù)庫(kù)作封裝時(shí),都會(huì)用的到2014-02-02C# HTTP認(rèn)證方式詳解與代碼實(shí)現(xiàn)
在C#中,HTTP認(rèn)證是客戶端與服務(wù)器之間進(jìn)行身份驗(yàn)證的一種機(jī)制,常見(jiàn)的HTTP認(rèn)證方式包括:Basic認(rèn)證、Digest認(rèn)證、OAuth、Bearer Token等,下面我們將從工作原理、優(yōu)缺點(diǎn)對(duì)比、代碼實(shí)現(xiàn)、案例實(shí)戰(zhàn)四個(gè)方面詳細(xì)介紹這些認(rèn)證方式,需要的朋友可以參考下2025-03-03C#使用Selenium+PhantomJS抓取數(shù)據(jù)
本文主要介紹了C#使用Selenium+PhantomJS抓取數(shù)據(jù)的方法步驟,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02C# 并發(fā)控制框架之單線程環(huán)境下實(shí)現(xiàn)每秒百萬(wàn)級(jí)調(diào)度
本文介紹了一款專為工業(yè)自動(dòng)化及機(jī)器視覺(jué)開發(fā)的C#并發(fā)流程控制框架,通過(guò)模仿Go語(yǔ)言并發(fā)模式設(shè)計(jì),支持高頻調(diào)度及復(fù)雜任務(wù)處理,已在多個(gè)項(xiàng)目中驗(yàn)證其穩(wěn)定性和可靠性2024-10-10