C#实现用户登录与注册程序
一、数据库表设计

二、程序结构

2.1、App类配置
在app.config中配置链接数据库的字段 <connectionStrings> 部分
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<connectionStrings><add name="con" connectionString="Data Source = WIN-20230216VRB; Initial Catalog = Test01; User ID = sa; Password=jk123"/></connectionStrings>
</configuration>
2.2、SelectMethod类
创建SelectMethod类,并在类中创建方法
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
class SelectMethod
{
//查询并返回值
public int Ex(string sql)
{
string con = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlConnection conn = new SqlConnection(con);
try
{
SqlCommand com = new SqlCommand(sql, conn);
if (conn.State == ConnectionState.Closed)
conn.Open();
com.CommandType = CommandType.Text;
//int result = Convert.ToInt32(com.ExecuteScalar());
int result = Convert.ToInt32(com.ExecuteScalar());
return result;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw;
}
finally
{
conn.Close();
}
}
//查询并返回数据集DataSet
public DataSet GetDataSet(string sql1)
{
string con = ConfigurationManager.ConnectionStrings["Con"].ToString();
SqlConnection conn = new SqlConnection(con);
DataSet ds = new DataSet();
try
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlDataAdapter sqlda = new SqlDataAdapter(sql1, conn);
sqlda.Fill(ds);
}
catch (Exception)
{
MessageBox.Show("未能连接到服务器", "提示");
}
finally
{
conn.Close();
}
return ds;
}
//添加
public int Add(string sql)
{
string con = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlConnection conn = new SqlConnection(con);
int resout;
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand(sql,conn);
resout = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw;
}
finally
{
conn.Close();
}
return resout;
}
}
}
2.3、窗体Form1
2.3.1、窗体From1的设计

2.3.2、窗体From1的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public event EventHandler SendMsgEvent; //使用默认的事件处理委托
public Form1()
{
InitializeComponent();
}
//判断登录用户和密码
private bool ValidateInput()
{
if (tbName.Text.Trim() == "")
{
MessageBox.Show("请输入登录用户名");
}
else if (tbName.Text.Length > 10)
MessageBox.Show("请输入正确的登录用户名");
else if (tbName.Text.Trim().Length > 5 && tbPWD.Text.Trim() == "")
MessageBox.Show("请输入密码");
return true;
}
//注册
private void btnApply_Click(object sender, EventArgs e)
{
btnApply btn = new btnApply();
btn.ShowDialog();
}
//登录
private void btnLog_Click(object sender, EventArgs e)
{
if (ValidateInput())
{
try
{
//string sql = $"select count(*) from userLOG where username='" + tbName.Text + "' and password = '" + tbPWD.Text + "'";
string sql = $"select count(*) from userLOG where username='{tbName.Text}' and password = '{tbPWD.Text}'";
SelectMethod select = new SelectMethod();
int num =select.Ex(sql);
if (num == 1)
{
this.Hide();
btnLog Form21 = new btnLog();
Form21.Show();
}
else
{
MessageBox.Show("用户名或密码不正确");
}
}
catch (Exception )
{
MessageBox.Show("未能连接到服务器", "提示");
}
}
}
}
}
2.4、窗体btnApply
2.4.1、窗体btnApply设计

2.4.2、窗体btnApply代码
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 WindowsFormsApp1
{
public partial class btnApply : Form
{
public btnApply()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = this.tbNameApply.Text;
string pwd = this.tbPWDApply.Text;
SelectMethod selectMethod = new SelectMethod();
string sql = $"insert into userLOG (username,password) values ('{name}','{pwd}')";
int re = selectMethod.Add(sql);
if (re > 0)
{
MessageBox.Show("注册成功");
}
else
{
MessageBox.Show("注册失败");
}
this.Close();
}
}
}
2.5、窗体btnLog
2.5.1、窗体btnLog的设计
在btnLog窗体中添加控件dataGriview

2.5.2、窗体btnLog的代码
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 WindowsFormsApp1
{
public partial class btnLog : Form
{
public btnLog()
{
InitializeComponent();
}
public static string PeopleName;
//登录成功后显示数据库中的查询信息
private void btnLog_Load(object sender, EventArgs e)
{
SelectMethod selectMethod = new SelectMethod();
string sql = $"select * from userLOG";
DataSet dataSet = selectMethod.GetDataSet(sql);
dataGridView1.DataSource = dataSet.Tables[0];
}
private void btnLog_Click(object sender, EventArgs e)
{
}
//关闭当前窗体关闭整个程序
private void btnLog_FormClosed(object sender, FormClosedEventArgs e)
{
this.Dispose();
this.Close();
Environment.Exit(0);
}
}
}
三、程序运行展示:
3.1、注册

3.2、登录成功后页面展示
