【MySQL】C# 连接MySQL

C# 连接MySQL

1. 添加MySQL引用

安装完MySQL之后,在安装的默认目录 C:\Program Files (x86)\MySQL\Connector NET 8.0 中查找MySQLData.dll文件。

在Visual Studio 中为项目中添加引用。

2. 引入命名空间

csharp 复制代码
using MySql.Data.MySqlClient;

3. 构建连接

csharp 复制代码
private static MySqlConnection m_Connect = null;
private static void Connect()
{
    string connectStr = "server=127.0.0.1;port=3306;database=test;user=root;password=root;";
    m_Connect = new MySqlConnection(connectStr);
}
server=IP地址;
port=端口号;
database=数据库名字;
user=管理员账号;
password=账号的密码;

4. 增删改查

csharp 复制代码
private static void Insert()
{
    try
    {
        m_Connect.Open();
        //string sqlStr = "Insert into users(username, password) values('takil', '789')";
        //string sqlStr = "Insert into users(username, password, registerdate) values('taksil', '789789', '2015-05-09')";
        string sqlStr = "Insert into users(username, password, registerdate) values('wilhelm', '78889', '" + DateTime.Now + "')";
       
        MySqlCommand cmd = new MySqlCommand(sqlStr, m_Connect);
        int result = cmd.ExecuteNonQuery();
        Console.WriteLine("成功影响了{0}条数据", result);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    {
        m_Connect?.Close();
    }
}

csharp 复制代码
private static void Delete()
{
    try
    {
        m_Connect.Open();
        string sql = "delete from users where username = 'takil'";

        
        MySqlCommand cmd = new MySqlCommand(sql, m_Connect);
        int result = cmd.ExecuteNonQuery();
        Console.WriteLine("成功影响了{0}条数据", result);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    { m_Connect?.Close(); }
}

csharp 复制代码
private static void Update()
{
    try
    {
        m_Connect.Open();
        string sql = "Update users set username = 'wqrwq', password = '123' where id = 5";

        MySqlCommand cmd = new MySqlCommand( sql, m_Connect);
        int result = cmd.ExecuteNonQuery();
        Console.WriteLine("成功影响了{0}条数据", result);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    { m_Connect?.Close(); }
}

csharp 复制代码
private static void Read()
{
    try
    {
        m_Connect.Open();
        string sqlStr = "select * from users";
        //string sqlStr = "select id, username, registerdate from users";
        
        MySqlCommand cmd = new MySqlCommand(sqlStr, m_Connect);
        MySqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            //Console.WriteLine(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString());
            //Console.WriteLine(reader.GetInt32(0) + " " + reader.GetString(1) + " " + reader.GetString(2));
            Console.WriteLine(reader.GetInt32("id") + " " + reader.GetString("username") + " " + reader.GetString("password"));
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    {
        m_Connect?.Close();
    }
}
csharp 复制代码
private static void ReadCount()
{
    try
    {
        m_Connect.Open();
        string sqlStr = "select Count(*) from users";
        MySqlCommand cmd = new MySqlCommand(sqlStr, m_Connect);

        //MySqlDataReader reader = cmd.ExecuteReader();
        //reader.Read();
        //int count = Convert.ToInt32(reader[0]);
        //Console.WriteLine("总数为{0}", count);

        object result = cmd.ExecuteScalar();
        int count = Convert.ToInt32(result);
        Console.WriteLine("总数为{0}", count);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    { m_Connect?.Close(); }
}

验证

csharp 复制代码
private static bool ValifyUser(string username, string password)
{
    try
    {
        m_Connect.Open();
        //string sqlStr = "select * from users where username = '" + username + "' and password = '" + password + "'";
        string sqlStr = "select * from users where username = @para1 and password = @para2";

        MySqlCommand cmd = new MySqlCommand(sqlStr, m_Connect);

        cmd.Parameters.AddWithValue("para1", username);
        cmd.Parameters.AddWithValue("para2", password);

        MySqlDataReader reader = cmd.ExecuteReader();
        if(reader.Read())
        {
            return true;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    {
        m_Connect?.Close();
    }
    return false;
}

因为作者精力有限,文章中难免出现一些错漏,敬请广大专家和网友批评、指正。

相关推荐
baivfhpwxf20234 分钟前
C# 5000 转16进制 字节(激光器串口通讯生成指定格式命令)
开发语言·c#
许嵩667 分钟前
IC脚本之perl
开发语言·perl
长亭外的少年18 分钟前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
直裾18 分钟前
Scala全文单词统计
开发语言·c#·scala
心仪悦悦19 分钟前
Scala中的集合复习(1)
开发语言·后端·scala
JIAY_WX21 分钟前
kotlin
开发语言·kotlin
代码小鑫1 小时前
A043-基于Spring Boot的秒杀系统设计与实现
java·开发语言·数据库·spring boot·后端·spring·毕业设计
久醉不在酒1 小时前
MySQL数据库运维及集群搭建
运维·数据库·mysql