欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中winform窗體實現(xiàn)注冊/登錄功能實例(DBHelper類)

 更新時間:2023年06月09日 09:28:28   作者:馬文杰。  
在編寫項目時,編寫了一部分關(guān)于登錄頁面的一些代碼,下面這篇文章主要給大家介紹了關(guān)于C#中winform窗體實現(xiàn)注冊/登錄功能(DBHelper類)的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下

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ù)庫幫助類”,在這里主要是進行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的管理員名稱,它默認是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("手機號不為空!");
            }
            if (pwd1.Text == "")
            {
                MessageBox.Show("密碼不為空!");
            }
            if (pwd2.Text == "")
            {
                MessageBox.Show("請確認密碼!");
            }
            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方法進行增加操作
                {
                    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("手機號不為空!");
            }
            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("請檢查手機號和密碼后重試!","登錄失敗");
                }
            }        }

手動跳轉(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("手機號不為空!");
            }
            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("請檢查手機號和密碼后重試!","登錄失敗");
                }
            }
        }
    }
}

登錄界面完整源碼

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("手機號不為空!");
            }
            if (pwd1.Text == "")
            {
                MessageBox.Show("密碼不為空!");
            }
            if (pwd2.Text == "")
            {
                MessageBox.Show("請確認密碼!");
            }
            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方法進行增加操作
                {
                    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)到登錄界面
        }
    }
}

驗證及注冊/登錄效果圖

密碼非空驗證

手機號非空驗證

驗證密碼一致

注冊完成后頁面

登錄成功界面

這個代碼可能有小bug,還請多多指教。

總結(jié)

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

相關(guān)文章

最新評論