1.创建C#控制台项目;

2.右键添加引用,添加C:\Program Files (x86)\MySQL\Connector NET 8.0\Assemblies\v4.5.2\MySql.Data.dll;

3.右键查看属性中的目标框架.NET Framework(注意框架版本);

4.编写建立连接代码;

using System;
using MySql.Data.MySqlClient;

namespace CSharp直接连接MySQL
{
    class Program
    {
        static void Main(string[] args)
        {
            //跟MySQL建立连接
            string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                Console.WriteLine("C#和MySQL已经建立连接");
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }
    }
}

ps: 数据库示例链接:https://pan.baidu.com/s/1WVHfNxhMnro1f-2TaamkOw
提取码:gz9m

本文地址:https://blog.csdn.net/qq_41603955/article/details/108573946