C# winform 连接mysql数据库(navicat)

1.解决方案资源管理器->右键->管理NuGet程序包->搜索, 安装Mysql.Data

2.解决方案资源管理器->右键->添加->引用->浏览->

C:\Program Files (x86)\MySQL\MySQL Installer for Windows

->选择->MySql.Data.dll

3.解决方案资源管理器->右键->添加->新建项->类->取名Dao(类名可以改)->加入代码

 public MySqlConnection sc;//数据库连接对象
        public MySqlConnection connect()//连接数据库
        {
            string str = String.Format("server=localhost;uid=root;pwd=123456;database=Book;charset=utf8");
            sc = new MySqlConnection(str);
            sc.Open();
            return sc;
        }
        public MySqlCommand command(string sql)//执行一条sql语句
        {
            MySqlCommand cmd = new MySqlCommand(sql, connect());
            return cmd;
        }
        public int Excute(string sql)//获取执行sql语句后 数据库表中数据条数的更新数量
        {
            return command(sql).ExecuteNonQuery();
        }
        public MySqlDataReader read(string sql)//读取数据库中的数据
        {
            return command(sql).ExecuteReader();
        }
        public void DaoClose()//关闭数据库
        {
            sc.Close();
        }

加入代码

using MySql.Data.MySqlClient;

server=服务器名;uid=用户名;pwd=密码;database=数据库;charset=utf8(可改为其它的编码)。

其中charset防止中文数据的插入会在数据库表中乱码显示。

Dao类文件完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Data.SqlClient;
using System.Data;
using MySql.Data.MySqlClient;

namespace ManageBook
{
    class Dao
    {
        public MySqlConnection sc;//数据库连接对象
        public MySqlConnection connect()//连接数据库
        {
            string str = String.Format("server=localhost;uid=root;pwd=123456;database=Book;charset=utf8");
            sc = new MySqlConnection(str);
            sc.Open();
            return sc;
        }
        public MySqlCommand command(string sql)//执行一条sql语句
        {
            MySqlCommand cmd = new MySqlCommand(sql, connect());
            return cmd;
        }
        public int Excute(string sql)//获取执行sql语句后 数据库表中数据条数的更新数量
        {
            return command(sql).ExecuteNonQuery();
        }
        public MySqlDataReader read(string sql)//读取数据库中的数据
        {
            return command(sql).ExecuteReader();
        }
        public void DaoClose()//关闭数据库
        {
            sc.Close();
        }
    }
}

向数据库中插入数据,注册例子

//注册按钮事件
        private void btnLogon_Click(object sender, EventArgs e)
        {
            //判读注册时所填入的信息是否有空项
            if (txtIDCard.Text == "" || txtName.Text == "" || txtTel.Text == "" || txtPwd.Text == "" || txtAgainPwd.Text == "" || cobSex.Text == "")
            {
                //提示:有空项
                MessageBox.Show("有空项","消息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            //注册   获取数据库中账号的最大值  然后加  1
            Dao dao = new Dao();
            dao.connect();//连接并打开数据库
            string sql = "select MAX(Uid) from T_User";
            MySqlDataReader reader = dao.read(sql);
            reader.Read();
            int id = int.Parse(reader[0].ToString())+1;//注册的账号
            string name = txtName.Text;
            string idCard = txtIDCard.Text;
            string tel = txtTel.Text;
            string sex = cobSex.Text;
            string pwd = txtPwd.Text;

            //进行添加
            sql = $"insert into T_User values('{id}','{name}','{pwd}','{sex}','{idCard}','{tel}','1')";
            if (dao.Excute(sql) > 0)
            {
                //注册成功
                MessageBox.Show("注册成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("注册失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            reader.Close();
            dao.DaoClose();
        }

如果sc.Open();打开数据库报错时,试试删除引用MySql.Data,再重新添加。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snow爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值