C#中winform窗體實現(xiàn)注冊/登錄功能實例(DBHelper類)
winform窗體應(yīng)用程序,實現(xiàn)注冊和登錄功能
1.1.開發(fā)環(huán)境:Visual Studio 2019 + SQL Server 2012 Management Studio
1.2.winform基本窗體界面

registeForm注冊界面

loginForm登錄界面
1.3.DBHelper類
DBHelper從字面上理解為“數(shù)據(jù)庫幫助類”,在這里主要是進(jìn)行winform窗體應(yīng)用程序和數(shù)據(jù)庫的數(shù)據(jù)交互。話不多說,我直接放在下面了
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語句</param>
/// <returns>返回DataSet數(shù)據(jù)表格</returns>
public static DataSet GetDataSet(string sql)
{
//創(chuàng)建數(shù)據(jù)庫連接對象
SqlConnection conn = new SqlConnection(connStr);
//創(chuàng)建數(shù)據(jù)適配器對象
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
//創(chuàng)建空數(shù)據(jù)表格對象
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
/// <summary>
/// 執(zhí)行增刪改查語句
/// </summary>
/// <param name="sql">增刪改sql語句</param>
/// <returns>返回增刪改執(zhí)行結(jié)果</returns>
public static bool ExecuteNonQuery(string sql)
{
SqlConnection conn = new SqlConnection(connStr);
//打開數(shù)據(jù)庫連接
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ù)庫連接對象
conn.Open();//打開數(shù)據(jù)庫連接
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ù)庫的名字;
uid是SQL server2012 Management Studio的管理員名稱,它默認(rèn)是sa;
pwd后面是SQL server2012 Management Studio的密碼,這里設(shè)置為123456;
1.4.建立數(shù)據(jù)庫
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語句建立數(shù)據(jù)庫,看起來很簡單吧。
1.5.主要功能代碼
注冊按鈕
//注冊按鈕
private void resBtn_Click(object sender, EventArgs e)
{
//非空驗證
if (tel.Text=="")//非空驗證
{
MessageBox.Show("手機(jī)號不為空!");
}
if (pwd1.Text == "")
{
MessageBox.Show("密碼不為空!");
}
if (pwd2.Text == "")
{
MessageBox.Show("請確認(rèn)密碼!");
}
if (pwd1.Text != pwd2.Text)//驗證密碼一致
{
MessageBox.Show("兩次輸入密碼不一致!");
}
else
{
string sql = string.Format(@"insert into oneTb(uTel,uPwd) values('{0}','{1}');",tel.Text, pwd1.Text);//添加的sql語句
if (DBHelper.ExecuteNonQuery(sql))//調(diào)用ExecuteNonQuery方法進(jìn)行增加操作
{
MessageBox.Show("用戶注冊成功,請登錄!");//注冊成功后提醒
LoginForm lf = new LoginForm();
lf.Show();//注冊完成后,自動跳轉(zhuǎn)到登錄界面
this.Hide();//隱藏該頁面
}
else
{
MessageBox.Show("注冊失敗,請重試!");//注冊失敗后提醒
}
}
}手動跳轉(zhuǎn)到登錄界面
//登錄按鈕--手動跳轉(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)
{
//非空驗證
if (tel.Text == "")//非空驗證
{
MessageBox.Show("手機(jī)號不為空!");
}
if (pwd.Text == "")//非空驗證
{
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)用查詢單個值的方法
if (i>0)
{
MessageBox.Show("登錄成功");
}
else
{
MessageBox.Show("請檢查手機(jī)號和密碼后重試!","登錄失敗");
}
} }手動跳轉(zhuǎn)到注冊界面
//注冊按鈕--手動跳轉(zhuǎn)到注冊界面
private void resBtn_Click(object sender, EventArgs e)
{
registForm rf = new registForm();
rf.Show();//跳轉(zhuǎn)到注冊界面
}注冊界面完整源碼
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();
}
//注冊按鈕--手動跳轉(zhuǎn)到跳轉(zhuǎn)到注冊界面
private void resBtn_Click(object sender, EventArgs e)
{
registForm rf = new registForm();
rf.Show();//跳轉(zhuǎn)到注冊界面
}
//登錄按鈕
private void LoginBtn_Click(object sender, EventArgs e)
{
//非空驗證
if (tel.Text == "")//非空驗證
{
MessageBox.Show("手機(jī)號不為空!");
}
if (pwd.Text == "")//非空驗證
{
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)用查詢單個值的方法
if (i>0)
{
MessageBox.Show("登錄成功");
}
else
{
MessageBox.Show("請檢查手機(jī)號和密碼后重試!","登錄失敗");
}
}
}
}
}登錄界面完整源碼
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();
}
//注冊按鈕
private void resBtn_Click(object sender, EventArgs e)
{
//非空驗證
if (tel.Text=="")//非空驗證
{
MessageBox.Show("手機(jī)號不為空!");
}
if (pwd1.Text == "")
{
MessageBox.Show("密碼不為空!");
}
if (pwd2.Text == "")
{
MessageBox.Show("請確認(rèn)密碼!");
}
if (pwd1.Text != pwd2.Text)//驗證密碼一致
{
MessageBox.Show("兩次輸入密碼不一致!");
}
else
{
string sql = string.Format(@"insert into oneTb(uTel,uPwd) values('{0}','{1}');", tel.Text, pwd1.Text);//添加的sql語句
if (DBHelper.ExecuteNonQuery(sql))//調(diào)用ExecuteNonQuery方法進(jìn)行增加操作
{
MessageBox.Show("用戶注冊成功,請登錄!");//注冊成功后提醒
LoginForm lf = new LoginForm();
lf.Show();//跳轉(zhuǎn)到登錄界面
this.Hide();//隱藏該頁面
}
else
{
MessageBox.Show("注冊失敗,請重試!");//注冊失敗后提醒
}
}
}
//登錄按鈕--手動跳轉(zhuǎn)到登錄界面
private void loginBtn_Click(object sender, EventArgs e)
{
LoginForm lf = new LoginForm();
lf.Show();//跳轉(zhuǎn)到登錄界面
}
}
}驗證及注冊/登錄效果圖

密碼非空驗證

手機(jī)號非空驗證

驗證密碼一致

注冊完成后頁面

登錄成功界面
這個代碼可能有小bug,還請多多指教。
總結(jié)
到此這篇關(guān)于C#中winform窗體實現(xiàn)注冊/登錄功能(DBHelper類)的文章就介紹到這了,更多相關(guān)winform窗體實現(xiàn)注冊登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實現(xiàn)方法
這篇文章主要介紹了C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實現(xiàn)方法,涉及C#基于Process類操作WinRar命令的相關(guān)實現(xiàn)技巧,代碼簡潔實用,需要的朋友可以參考下2016-06-06
C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法
這篇文章主要介紹了C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法,以實例形式分析了創(chuàng)建access數(shù)據(jù)庫及在access數(shù)據(jù)庫中建表的完整過程,是非常實用的技巧,需要的朋友可以參考下2014-12-12
C# KeyUp事件中MessageBox的回車(Enter)鍵回調(diào)問題解決方案
這篇文章主要介紹了C# KeyUp事件中MessageBox的回車(Enter)鍵回調(diào)問題解決方案,需要的朋友可以參考下2014-07-07
c#標(biāo)準(zhǔn)idispose模式使用示例
下面將把C#里實現(xiàn)IDispose模式的代碼展現(xiàn)出來,大家一起來學(xué)習(xí)一下,它的使用場合也很多的,當(dāng)我們手動對網(wǎng)站,數(shù)據(jù)庫作封裝時,都會用的到2014-02-02
C#使用Selenium+PhantomJS抓取數(shù)據(jù)
本文主要介紹了C#使用Selenium+PhantomJS抓取數(shù)據(jù)的方法步驟,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
C# 并發(fā)控制框架之單線程環(huán)境下實現(xiàn)每秒百萬級調(diào)度
本文介紹了一款專為工業(yè)自動化及機(jī)器視覺開發(fā)的C#并發(fā)流程控制框架,通過模仿Go語言并發(fā)模式設(shè)計,支持高頻調(diào)度及復(fù)雜任務(wù)處理,已在多個項目中驗證其穩(wěn)定性和可靠性2024-10-10

