c# mysql登录界面_C#_连接数据库实现 登录注册界面

本文介绍如何使用C#编程实现一个连接MySQL数据库的登录和注册界面。登录逻辑通过验证用户名和密码来确定用户身份,注册功能则检查用户输入的账号是否已存在,并确保密码和验证码正确。在登录失败3次后,程序会提示用户并退出。
摘要由CSDN通过智能技术生成

//编写登录界面逻辑usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceLoginDatabase

{public partial classLogin : Form

{private int errorTime = 3;publicLogin() {

InitializeComponent();

}private void loginBtn_Click(objectsender, EventArgs e) {

errorTime= errorTime - 1;string username = txtName.Text.Trim(); //取出账号

string pw = txtPwd.Text.Trim(); //取出密码

string constr = "Server=.;DataBase=SU; Integrated Security=True"; //设置连接字符串

SqlConnection mycon = new SqlConnection(constr); //实例化连接对象

mycon.Open();

SqlCommand mycom= mycon.CreateCommand(); //创建SQL命令执行对象

string s1 = "select account,password from register where account='" + username + "' and password='" + pw + "'"; //编写SQL命令

mycom.CommandText = s1; //执行SQL命令

SqlDataAdapter myDA = new SqlDataAdapter(); //实例化数据适配器

myDA.SelectCommand = mycom; //让适配器执行SELECT命令

DataSet myDS = new DataSet(); //实例化结果数据集

int n = myDA.Fill(myDS, "register"); //将结果放入数据适配器,返回元祖个数

if (n != 0) {if (checkCode.Text ==textCheck.Text) {

MessageBox.Show("欢迎使用!"); //登录成功

this.Close();

}else{

MessageBox.Show("验证码填写错误");

textCheck.Text= "";

}

}else

if (errorTime < 3) {

MessageBox.Show("用户名或密码有错。请重新输入!还有" + errorTime.ToString() + "次机会");

txtName.Text= ""; //清空账号

txtPwd.Text = ""; //清空密码?

txtName.Focus(); //光标设置在账号上

} else{

MessageBox.Show("你输入的用户名或密码已达三次? 将退出程序");this.Close();

}

}private void cancelBtn_Click(objectsender, EventArgs e) {

Application.Exit();

}private void button1_Click(objectsender, EventArgs e) {

Register register= newRegister();

register.ShowDialog();

}private void checkCode_Click(objectsender, EventArgs e) {

Random random= newRandom();int minV = 12345, maxV = 98765;

checkCode.Text=random.Next(minV, maxV).ToString();

}

}

}5.编写注册界面逻辑usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceLoginDatabase

{public partial classRegister : Form

{publicRegister() {

InitializeComponent();

}private void btnRegister_Click(objectsender, EventArgs e) {//检查是否已经存在

string userID = userId.Text.Trim(); //取出账号

/**

* 连接数据库*/

string constr = "Server=.;DataBase=SU; Integrated Security=True"; //设置连接字符串

SqlConnection mycon = new SqlConnection(constr); //实例化连接对象

mycon.Open();//查询新注册的用户是否存在

SqlCommand checkCmd = mycon.CreateCommand(); //创建SQL命令执行对象

string s = "select account from register where account='" + userID + "'";

checkCmd.CommandText=s;

SqlDataAdapter check= new SqlDataAdapter(); //实例化数据适配器

check.SelectCommand = checkCmd; //让适配器执行SELECT命令

DataSet checkData = new DataSet(); //实例化结果数据集

int n = check.Fill(checkData, "register"); //将结果放入数据适配器,返回元祖个数

if (n != 0) {

MessageBox.Show("用户名存在");

userId.Text= ""; userPw.Text = "";

nickName.Text= "";

}//确认密码

if (ensurePw.Text !=userPw.Text) {

ensurePw.Text= "";

}//验证码

if (textCheck.Text !=checkCode.Text) {

textCheck.Text= "";

}//插入数据SQL 逻辑

string s1 = "insert into Register(account,password,nickname) values ('" + userId.Text + "','" + userPw.Text + "','"

+ nickName.Text + "')"; //编写SQL命令

SqlCommand mycom = new SqlCommand(s1, mycon); //初始化命令

mycom.ExecuteNonQuery(); //执行语句

mycon.Close(); //关闭连接

mycom = null;

mycon.Dispose();//释放对象

if (userId.Text == "" || userPw.TextLength <= 6 || nickName.Text == "" || ensurePw.Text == ""

|| textCheck.Text == "") {

MessageBox.Show("请将信息填完整");

}else{

MessageBox.Show("注册成功");this.Close();

}

}private void checkCode_Click(objectsender, EventArgs e) {

Random random= newRandom();int minV = 12345, maxV = 98765;

checkCode.Text=random.Next(minV, maxV).ToString();

}

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C#MySQL 实现登录界面的示例代码: 1. 首先,需要在项目中添加 MySQL .NET Connector 引用。可以通过 NuGet 包管理器来进行安装。 2. 创建一个 Windows 窗体应用程序,在窗体上放置用户名和密码的文本框、登录按钮等控件。 3. 在窗体的代码中,需要导入 MySQL Connector 的命名空间: ```csharp using MySql.Data.MySqlClient; ``` 4. 在登录按钮的 Click 事件处理函数中,编写连接 MySQL 数据库的代码: ```csharp private void btnLogin_Click(object sender, EventArgs e) { string connectionString = "server=localhost;user=root;database=mydb;password=mypassword"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); MySqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM users WHERE username = @username AND password = @password"; command.Parameters.AddWithValue("@username", txtUsername.Text); command.Parameters.AddWithValue("@password", txtPassword.Text); MySqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { MessageBox.Show("登录成功!"); } else { MessageBox.Show("用户名或密码错误!"); } connection.Close(); } ``` 5. 在上面的代码中,需要根据实际情况修改连接字符串中的服务器地址、数据库名称、用户名和密码。 6. 在 SELECT 查询语句中,@username 和 @password 是参数化查询的占位符,可以有效避免 SQL 注入攻击。 7. 使用 ExecuteReader 方法执行查询语句,并使用 Read 方法读取结果集中的第一行数据。如果能够成功读取,则说明用户名和密码匹配,登录成功;否则登录失败。 8. 最后,关闭数据库连接。 以上就是使用 C#MySQL 实现登录界面的基本步骤和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值