网上搜索出来的很乱。最后还是直接去mysql官网找到的答案。

下载什么东西?

首先需要下载连接mysql用的库文件。

点开这个网址:  https://dev.mysql.com/downloads/connector/net/   选择.NET和Mono 下载解压就行。

c#怎么连接MySQL_右键

怎么用?

1、导入库文件

我是直接全部都导入进去的。

【引用】右键,【添加引用】。

c#怎么连接MySQL_右键_02

 

点击【浏览】,然后全选,【添加】就行。

 

c#怎么连接MySQL_右键_03

 

2、代码怎么敲?看下面这个网址的描述就行。可能以后不能用了,到时候就自己在官网上找吧。要是看不懂英文就用一下翻译。

https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-connection.html

c#怎么连接MySQL_库文件_04

using System;
using System.Data;

using MySql.Data;
using MySql.Data.MySqlClient;

public class Tutorial1
{
    public static void Main()
    {
        string connStr = "server=localhost;user=root;database=world;port=3306;password=******";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();
            // Perform database operations
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        conn.Close();
        Console.WriteLine("Done.");
    }
}

c#怎么连接MySQL_库文件_05

效果是啥?

c#怎么连接MySQL_右键_06

就这样。再见!