首页 > 解决方案 > 错误连接到远程 SQL Server Express 2012 windows 10 c#

问题描述

我在 Windows 10 64 位上安装了 SQL Server Express 2012,来自同一台计算机的连接工作正常,当我从同一网络上的另一台计算机连接时出现问题,我收到错误消息:

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 connection
-provider TCP provider error 0 the wait operation timeout

我尝试了以下事情,但没有解决问题:

这是我的代码:

        public static bool TestSQLServerConnection(
            string ComputerNameOrIPAddress,
            string SQLServerInstanceName,
            string PortNumber)
        {

            try
            {
                
                SqlConnectionStringBuilder SQLServerConnectionString = new SqlConnectionStringBuilder();
                SQLServerConnectionString.DataSource = ComputerNameOrIPAddress+",+"+PortNumber+"\\"+SQLServerInstanceName;
                SQLServerConnectionString.InitialCatalog = DatabseName;
                SQLServerConnectionString.TrustServerCertificate = true;
                SQLServerConnectionString.IntegratedSecurity = true;
                
                //SQLServerConnectionString.UserID = "...";
                //SQLServerConnectionString.NetworkLibrary = "DBMSSOCN";
                //SQLServerConnectionString.Password = "...";

                SqlConnection con = new SqlConnection(SQLServerConnectionString.ConnectionString);
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText=  "select 1";
                con.Open();
                cmd.ExecuteScalar();

                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

当我ping它连接的SQL Server的计算机的IP地址或计算机名称并返回响应时,表示计算机已连接到网络。我在没有任何解决方案的情况下搜索,请帮助我。谢谢

标签: c#sql-servernetworkingconnection-string

解决方案


我在防火墙上添加了 SQL 浏览器服务 UDP 端口 1434 并且工作正常。 使用 SQL Server Management Studio 远程连接到托管在 Azure 虚拟机上的 SQL Server Express 实例


推荐阅读