C#簡單注冊登錄實現

1、創建Company數據庫中的UserInfo表,其中字段包括ID、UserName、UserPassword、UserAge、UserSex

USE Company
CREATE TABLE UserInfo
(
ID INT NOT NULL PRIMARY KEY IDENTITY(1,1),
UserName NVARCHAR(32),
UserPassword NVARCHAR(32),
UserAge INT NOT NULL DEFAULT(18),
UserSex NCHAR(8)
)

2、數據庫連接字符串存放在應用程序的配置文件中,通過configurationManager類的ConnectionStrings屬性讀取配置文件中XML格式的鍵值對,從而拿到數據庫連接字符串。

引入System.Configuration類庫,導入ConfigurationManager類的命名空間System.Configuration。
using System.Configuration;

創建數據庫連接字符串

string strCon = ConfigurationManager.ConnectionStrings["SQLString"].ConnectionString;

3、主界面注冊按鈕單擊彈出注冊界面

        private void RegistButton_Click(object sender, EventArgs e)
        {
            RegistForm regFrm = new RegistForm();
            regFrm.Show();
        }
彈出注冊界面.jpg

注冊界面代碼

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;
using System.Configuration;
using System.Data.SqlClient;

namespace UserFormExample
{
    public partial class RegistForm : Form
    {
        public RegistForm()
        {
            InitializeComponent();
        }

        //注冊按鈕
        private void RegistButton_Click(object sender, EventArgs e)
        {
            //調用CheckFrmTxt函數簡單校驗數據
            if (!CheckFrmTxt())
            {
                return;
            }
            //通過ConfigurationManager類從配置文件中讀取數據庫連接字符串
            string strCon = ConfigurationManager.ConnectionStrings["SQLString"].ConnectionString;
            //創建數據庫連接對象
            using (SqlConnection connect = new SqlConnection(strCon))
            {
                //打開數據庫
                connect.Open();
                //構建SQL命令對象
                using (SqlCommand cmd = connect.CreateCommand())
                {
                    //構建SQL插入數據命令
                    string strSql = string.Format($"insert into UserInfo(UserName,UserPassword,UserAge,UserSex)values('{this.UserNameTextBox.Text}','{this.PasswordTextBox.Text}','{int.Parse(this.AgeTextBox.Text)}','{this.SexTextBox.Text}')");
                    cmd.CommandText = strSql;
                    //執行非查詢SQL命令
                    cmd.ExecuteNonQuery();
                }
            }
            MessageBox.Show("注冊成功");
        }
        private bool CheckFrmTxt()
        {
            if (string.IsNullOrEmpty(this.UserNameTextBox.Text.Trim()))
            {
                MessageBox.Show("用戶名不能為空!");
                return false;
            }
            return true;
        }
    }
}
注冊.jpg

4、登錄界面(主界面)代碼

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;
using System.Data.SqlClient;
using System.Configuration;

namespace UserFormExample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            //簡單校驗數據
            if (string.IsNullOrEmpty(this.UserNameTextBox.Text.Trim())||string.IsNullOrEmpty(this.PasswordTextBox.Text.Trim()))
            {
                MessageBox.Show("請輸入正確的用戶名和密碼");
                return;
            }
            //構建數據庫連接對象
            string strCon = ConfigurationManager.ConnectionStrings["SQLString"].ConnectionString;
            using (SqlConnection connect = new SqlConnection(strCon))
            {
                //打開數據庫
                connect.Open();
                //構建SQL命令對象
                using (SqlCommand cmd = connect.CreateCommand())
                {
                    //構建查詢SQL命令
                    string strSql = string.Format($"select count(1) from UserInfo where UserName='{this.UserNameTextBox.Text}' and UserPassword='{this.PasswordTextBox.Text}'");
                    cmd.CommandText = strSql;
                    //執行查詢命令,返回第一行第一列的值為Object類型
                    object result = cmd.ExecuteScalar();
                    int rows = int.Parse(result.ToString());
                    if (rows>=1)
                    {
                        MessageBox.Show("登錄成功");
                    }
                    else
                    {
                        MessageBox.Show("登錄失敗");
                    }
                }
            }
        }

        private void RegistButton_Click(object sender, EventArgs e)
        {
            RegistForm regFrm = new RegistForm();
            regFrm.Show();
        }
    }
}
登錄.jpg
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容