连接到本地MySQL服务器时出现问题 [英] Problems connecting to local MySQL server

查看:152
本文介绍了连接到本地MySQL服务器时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在连接到本地MySQL数据库时遇到问题.

I have problems connecting to a local MySQL database.

我确保服务器正在运行,并且可以使用连接字符串中的用户名和密码连接到服务器,并使用终端创建数据库.

I made sure the server is running and i can connect to the server using the user and password from the connection string and create databases with the terminal.

我正在Mac和MySQL 8.0.16上使用Visual Studio社区.

I'm using visual studio community on a mac and MySQL 8.0.16.

示例: Program.cs

Example: Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ReadSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            DataAccess dataAccess = new DataAccess();
            dataAccess.GetTask();
        }
    }
}

DataAccess.cs

DataAccess.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace ReadSQL
{
    public class DataAccess
    {
        public List<Task> GetTask()
        {
            using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("workloadDB")))
            {
                var output = connection.Query<Task>($"select * from task where id=1").ToList(); //exception thrown here
                Console.WriteLine(output);           
            }
        }
     }
}

Helper.cs

Helper.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadSQL
{

        public static class Helper
        {
            public static string CnnVal(string name)
            {
                return ConfigurationManager.ConnectionStrings[name].ConnectionString;
            }
        } 
}

Task.cs

using System;
namespace ReadSQL
{
    public class Task
    {
        int id { get; set; }
        string name { get; set; }
    }
}

App.config

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings>
        <add name="workloadDB" connectionString="Server=localhost;Database=workload;Uid=root;Pwd=adminPasswort;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

我希望变量输出包含表的第一行,但我却收到错误消息:

I expect the variable output to contain the first row of my table, but instead i get the error message:

System.Data.SqlClient.SqlException 与网络相关或特定于实例的错误发生在 建立与sql server的连接.找不到服务器或 无法访问.验证实例名称正确,并且 SQL Server配置为允许远程连接.

System.Data.SqlClient.SqlException 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.

我不知道为什么无法建立连接,因为MySQL Server肯定正在运行,并且连接字符串应该正确(也尝试过:trusted_connection = true;). 在此先感谢!

I don't know why i cannot establish the connection, because the MySQL Server is definitely running and the connection string should be correct (also tried: trusted_connection=true;). Thanks in Advance!

推荐答案

您不能使用SqlConnection类连接到MySQL.而是从NuGet安装 MySqlConnector ,然后使用:

You cannot use the SqlConnection class to connect to MySQL. Instead, install MySqlConnector from NuGet, then use:

using (IDbConnection connection = new MySqlConnection(Helper.CnnVal("workloadDB")))
{
    var output = connection.Query<Task>($"select * from task where id=1").ToList();          
}

您的连接字符串看起来应该与MySqlConnection兼容,但是如果您使用任何其他选项,则应根据

Your connection string looks like it should be compatible with MySqlConnection, but if you're using any other options you should double-check them against the list of valid connection options.

这篇关于连接到本地MySQL服务器时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆