无法连接到mysql服务器并且不知道原因

vfwfrxfs  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(266)

这个问题在这里已经有答案了

asp.net使用sqlconnection连接mysql(3个答案)
9个月前关门了。
我正在尝试连接到mysql服务器
mysql工作台截图:

using System;
using System.Data.SqlClient;

namespace C_sharp_test
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string connectionString = "server=localhost;uid=root;pwd=***********;database=terra_incognita_online";
                SqlConnection sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = connectionString;
                sqlConnection.Open();
                Console.WriteLine("open");
                sqlConnection.Close();
                Console.WriteLine("close");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

我得到这个错误

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

我不知道该怎么办,请帮帮我

kr98yfug

kr98yfug1#

将“sqlconnection”替换为“mysqlconnection”,这样就可以了。

namespace C_sharp_test
 {
    class Program
    {
        static void Main(string[] args)
        {
           try
           {
               string connectionString = "server=localhost;uid=root;pwd=***********;database=terra_incognita_online";
               MySqlConnection sqlConnection = new MySqlConnection();
               sqlConnection.ConnectionString = connectionString;
               sqlConnection.Open();
               Console.WriteLine("open");
               sqlConnection.Close();
               Console.WriteLine("close");
           }
           catch (Exception e)
           {
               Console.WriteLine(e);
           }
       }
   }
}

另外,安装nuget包mysql.data;
并添加引用

using MySql.Data.MySqlClient;
vngu2lb8

vngu2lb82#

您正在尝试使用sql server的类连接到mysql数据库服务器。他们使用不同的协议,所以这不起作用(而且几乎肯定永远不会起作用)。
如果您想使用mysql而不是sqlserver,请查看mysql connector for.net。

相关问题