实际效果图:
主页面:
在windows窗体设计中,主要运用的有,label, textBox,Button
连接数据库(以Sql Server为例):
1.如下图表示数据库已经与项目建立连接,我们要保证已经建立好一个包含username,password的 user表,并在表中插入一定的数据,方便后面的使用
2.如何在代码中与数据库建立连接?
//数据库配置
string strcon = "Data Source=LAPTOP-TOLRRRC6\\SQLEXPRESS;database = master;Integrated Security=True";
SqlConnection con = new SqlConnection(strcon);
//连接
con.Open();
Data Source = 服务器名;Initial Catalog = 数据库名;User ID = 用户名;Pwd = 密码(没有密码可以省略)
与sql server建立连接的时候,可以在连接属性中,可以在连接字符串一栏查看数据库连接
代码实现:
登录(Login.cs):
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Login
{
/// <summary>
/// 连接数据库实现用户的登录注册
/// 时间:2022-11-23
/// 作者:Spraing※boy
/// </summary>
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
/// <summary>
/// 连接数据库实现用户的登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//获取用户输入的数据
string username = this.txtusername.Text;
string password = this.txtpassword.Text;
if (username.Equals("") || password.Equals(""))
{
MessageBox.Show("用户名或密码不能为空!");
} else
{
//数据库配置
string strcon = "Data Source=LAPTOP-TOLRRRC6\\SQLEXPRESS;database = master;Integrated Security=True";
SqlConnection con = new SqlConnection(strcon);
//连接
con.Open();
string sql = "Select * from [user] where username='" + username + "' and password ='" + password + "'";
//执行Sql语句
SqlCommand command = new SqlCommand(sql, con);
//获取或设置一个值,该值指示解释 CommandText 属性的方式。
command.CommandType = CommandType.Text;
//提供一种从 SQL Server 数据库中读取只进的行流的方式
SqlDataReader sdr;
//将 CommandText 发送到 Connection,并生成 SqlDataReader。
sdr = command.ExecuteReader();
//Read()返回boolean 如果存在更多行,则为 true;否则为 false。
if (sdr.Read())
{
MessageBox.Show("登录成功");
} else
{
MessageBox.Show("用户名或密码错误,登录失败");
}
}
}
/// <summary>
/// 注册按钮,主要功能为跳转到注册的页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRegister_Click(object sender, EventArgs e)
{
//隐藏本窗体,打开注册窗体
this.Hide();
new Register().Show();
}
}
}
注册(Register.cs):
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Login
{
public partial class Register : Form
{
public Register()
{
InitializeComponent();
}
/// <summary>
/// 连接数据库实现用户的注册
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRegister_Click(object sender, EventArgs e)
{
string Rusername = this.txtRusername.Text;
string Rpassword = this.txtRpassword.Text;
string Rconfirmpwd = this.txtRconfrimpwd.Text;
//数据库配置
string strcon = "Data Source=LAPTOP-TOLRRRC6\\SQLEXPRESS;database = master;Integrated Security=True";
SqlConnection con = new SqlConnection(strcon);
//连接
con.Open();
//查看新用户是否存在
string sqlCheck = "select username from [user] where username='" + Rusername + "'";
SqlCommand checkcommand = new SqlCommand(sqlCheck, con);
checkcommand.CommandType = CommandType.Text;
SqlDataReader reader;
reader = checkcommand.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("该用户名已存在,请重新输入");
}
else if (Rusername.Equals("") || Rpassword.Equals("") || Rconfirmpwd.Equals("") )
{
MessageBox.Show("你输入的信息不完整,请重新输入");
}
else if (!Rpassword.Equals(Rconfirmpwd))
{
MessageBox.Show("你两次输入密码不一致,请重新输入");
}
else
{
//关闭DataReader,否则会影响insertCommand的执行
reader.Close();
//向数据库中插入用户数据
string s1 = "insert into [user](username,password) values ('" + Rusername + "','" + Rpassword + "')";
SqlCommand insertCommand = new SqlCommand(s1, con); //初始化命令
insertCommand.ExecuteNonQuery(); //执行命令
insertCommand = null;
MessageBox.Show("注册成功");
//关闭数据库的连接
con.Close();
new Login().Show();
this.Hide();
}
}
}
}