C#连接Mysql实现登录注册界面
Posted F1gh4
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#连接Mysql实现登录注册界面相关的知识,希望对你有一定的参考价值。
界面如上:
是一个初始登录界面,有注册和登录功能,使用mysql数据库:
注册界面:
登录界面模板:
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 MySql.Data.MySqlClient;
using System.Data.SqlClient;
namespace WinForms_Medicine_test
public partial class Form1 : Form
private int error_time = 3;
public Form1()
InitializeComponent();
private void textBox1_TextChanged(object sender, EventArgs e)
private void button1_Click(object sender, EventArgs e)
label4.Text = "登录中。。。请稍候";
Form2 f2 = new Form2();
f2.showuser = this.textBox1.Text.Trim();
//------------------连接数据库字符串-----------------------------------
String serverinfo = "server=your_server;" +
"port=your_port;" +
"user=your_username;" +
"password=your_passwd;" +
"database=;";
//------------------连接数据库字符串-----------------------------------
//异常检测
string username = textBox1.Text.Trim();
string passwd = textBox2.Text.Trim();
if (username.Equals("") || passwd.Equals(""))
MessageBox.Show("用户名或密码不能为空!");
else
MySqlConnection conn = new MySqlConnection(serverinfo); //建立连接
try
conn.Open(); //打开
//建立sql语句
string sql = "select count(*) from userINFO.loginINFO where name = '"
+ username + "' and passwd = '"
+ passwd + "'";
MySqlCommand com = new MySqlCommand(sql, conn);
int resp = Convert.ToInt32(com.ExecuteScalar()); //判断是否存在
if (resp > 0)
label4.Text = "";
MessageBox.Show("登录成功!");
this.Hide();
f2.Show();
conn.Close();//关闭连接
else
error_time = error_time - 1; //错误时间
MessageBox.Show("用户名或密码错误!!");
label4.Text = "";
catch (Exception ex)
MessageBox.Show("出现了未知的错误,请联系管理员.");
label4.Text = "";
private void textBox2_TextChanged(object sender, EventArgs e)
private void label1_Click(object sender, EventArgs e)
private void Form1_Load(object sender, EventArgs e)
private void button2_Click(object sender, EventArgs e)
Form3 f3 = new Form3();
this.Hide();
f3.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 MySql.Data.MySqlClient;
using System.Data.SqlClient;
namespace WinForms_Medicine_test
public partial class Form3 : Form
public Form3()
InitializeComponent();
private void label4_Click(object sender, EventArgs e)
private void textBox1_TextChanged(object sender, EventArgs e)
private void button2_Click(object sender, EventArgs e)
Form1 f1 = new Form1();
this.Hide();
f1.Show();
public string nickname = "";
public string username = "";
private void button1_Click(object sender, EventArgs e)
label5.Text = "注册中。。。请稍候";
//------------------连接数据库字符串-----------------------------------
String serverinfo = "server=your_server;" +
"port=your_port;" +
"user=your_username;" +
"password=your_passwd;" +
"database=;";
//------------------连接数据库字符串-----------------------------------
string username = textBox1.Text.Trim();
string passwd = textBox2.Text.Trim(); //获取输入的注册用户名和密码
string nickname = textBox3.Text.Trim();
MySqlConnection conn = new MySqlConnection(serverinfo); //建立连接
try
conn.Open(); //打开
//建立sql语句
string sql_1 = "select count(*) from userINFO.loginINFO where name = '"+ username+"'";
string sql_2 = "select count(*) from userINFO.loginINFO where name = '" + nickname + "'";
MySqlCommand com_1 = new MySqlCommand(sql_1, conn);
MySqlCommand com_2 = new MySqlCommand(sql_2, conn);
int resp_1 = Convert.ToInt32(com_1.ExecuteScalar()); //分别判断是否存存在
int resp_2 = Convert.ToInt32(com_2.ExecuteScalar());
if (resp_1 > 0)
MessageBox.Show("用户名已存在!请重新输入!");
label5.Text = "";
else
if(resp_2 > 0)
MessageBox.Show("昵称已存在,请重新输入");
label5.Text = "";
else
String sql_insert = "insert into userINFO.loginINFO(name,passwd,nickname) VALUES('" + username + "','" + passwd +"','"+nickname + "')";
MySqlCommand com_3 = new MySqlCommand(sql_insert, conn);
com_3.ExecuteNonQuery(); //执行
MySqlCommand com_4 = new MySqlCommand(sql_1, conn); //检验数据库里是否加入了注册数据
com_4.ExecuteNonQuery(); //执行
int resp_4 = Convert.ToInt32(com_4.ExecuteScalar());
com_3 = null;
if (resp_4 > 0)
MessageBox.Show("恭喜! " + nickname + ",注册成功,快去登陆吧!");
label5.Text = "";
Form1 f1 = new Form1();
this.Hide();
f1.Show();
else
MessageBox.Show("注册失败!请重试!");
label5.Text = "";
conn.Close();//最后关闭连接
catch (Exception ex)
MessageBox.Show("出现了未知的错误,请联系管理员.");
label5.Text = "";
private void Form3_Load(object sender, EventArgs e)
以上是关于C#连接Mysql实现登录注册界面的主要内容,如果未能解决你的问题,请参考以下文章