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();
}
注册界面代码
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;
}
}
}
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();
}
}
}