前言:
1.主窗体为登录界面,输入用户名和密码,单击“登录”按钮时从数据库中读取数据,与输入的用户名、密码验证,验证通过后,登录窗体消失。单击“注册”按钮时窗体中的输入的数据保存到数据库中 。
环境:
Visual Studio 2019 + MySQL8.0
【只需要在vs的下面项目中引用MySql.Data.dll文件,添加数据库语言就可以连接数据库了】
效果展示



实现过程
1.创建项目 WinForm窗体自行实现
2.文件链接



3.数据库截图

4.C#代码
绿色版【不懂代码可以见下一篇文章有注释】
添加using MySql.Data.MySqlClient;语句

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;

namespace WindowsFormsApp4
{ 
 	public partial class Form1 : Form
    	{
        	public Form1()
       		{
           		 InitializeComponent();
       		}
         	private void button1_Click(object sender, EventArgs e)
        	{
        		//登录
                    	string userName = this.txtUserName.Text;
            		string userPassword = this.txtPassword.Text;
			
			if (userName.Equals("") || userPassword.Equals(""))
            		{
                		MessageBox.Show("用户名或密码不能为空!");
           		 }
           		 else{	
				 string strcon = "server=localhost;database=login;uid=root;pwd=12345;";
                		MySqlConnection con = new MySqlConnection(strcon);
                		try{
					con.Open();
 					string sqlSel = "select count(*) from login.test where userName = '" + userName + "' and password = '" + userPassword + "'";
  					MySqlCommand com = new MySqlCommand(sqlSel, con);
					if (Convert.ToInt32(com.ExecuteScalar()) > 0)
                   			{
                        			MessageBox.Show("登录成功!");
                        			/*this.DialogResult = DialogResult.OK;
                       				  this.Dispose();
                        			  this.Close();*/
                        			Form2 next = new Form2();//创建将要打开的窗体对象
                        			next.Show(); // 打开新窗体--Show非模式对话框--ShowDialog模式对话框
                        			this.Hide();// this.Close()关闭当前窗体,因为登录是主窗体所以不能关闭
                       				 //用户名和密码验证错误,提示错误。
                    			else
                    			{
                      				 MessageBox.Show("用户名或密码错误!");
                    			}
				}
				catch (Exception ex)
               			 {
                  			  MessageBox.Show(ex.Message.ToString() + "打开数据库失败");
               			 }
			}
         	}
 		private void button2_Click(object sender, EventArgs e)
        	{
           		//注册
           		string userName = this.txtUserName.Text;
            		string userPassword = this.txtPassword.Text;
 	    		string strcon = "server=localhost;database=login;uid=root;pwd=12345;";
 	    
           		MySqlConnection con = new MySqlConnection(strcon);
            		con.Open();
            		String sql = "INSERT INTO test(userName,password) VALUES('" + userName + "','" + userPassword + "')"; // 没有判断重复插入
            
            		MySqlCommand cmd = new MySqlCommand(sql, con);
            		cmd.ExecuteNonQuery();
            		MessageBox.Show("注册成功");
            
            		Form1 next01 = new Form1();//创建将要打开的窗体对象
            		next01.Show(); // 打开新窗体--Show非模式对话框--ShowDialog模式对话框
            		this.Hide();// this.Close()关闭当前窗体,因为登录是主窗体所以不能关闭
          
            		con.Close();
		}

}

更多推荐

C#连接mysql数据库实现登陆注册界面【小白教程】