将 Windows VM 连接到 mac 本地 SQL 数据库服务器时出错

问题描述 投票:0回答:1

尝试连接到我的 SQL 数据库时出现以下错误:

System.Data.SqlClient.SqlException:“建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)'


这是我的设置:

  • 我有一个 MacBook m2 pro 芯片。
  • 我正在通过 Mac 上的 azure data studio 在 docker 上运行 SQL 数据库
  • 我正在 Parallels(Windows 虚拟机)上运行 Visual Studio

这是我的代码:

using Dapper;
using SQLlessions.Models;
using System.Data;
using System.Data.SqlClient;

namespace SQLlessions
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            // 1 - connect to sql database

            string connectionString = "Server = localhost; Database = DotNetCourseDatabase; Trusted_Connection = false; TrustServerCertificate = true; User Id = **; Password = *******;";

            IDbConnection dbConnection = new SqlConnection(connectionString);
            
            // 2 - test the connection works

            string sqlCommand = "SELECT GETDATE()";

            DateTime rightNow = dbConnection.QuerySingle<DateTime>(sqlCommand);

            Console.WriteLine(rightNow);


        }
    }
}

我什么都试过了!我用 google 搜索并尝试了所有方法,但似乎无法连接到 SQL 数据库。

parallels azure-data-studio
1个回答
0
投票
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)

启用与 SQL Server 的远程连接,如下所示:

右键单击服务器,转到属性,转到连接,然后启用

Allow remote connection to the server
,如下所示:

enter image description here

当您运行代码时,您将能够成功连接到 SQL 服务器,如下所示:

enter image description here

此外,在连接到 SQL 服务器时检查以下选项:

  • 确保网络防火墙允许通过端口 1433 的出站流量。
  • 确保您提供了 SQL 服务器的正确详细信息。
  • 确保在 SQL 实例上启用 NP。

欲了解更多信息,您可以参考这个MS文档

© www.soinside.com 2019 - 2024. All rights reserved.