连接到其他PC上的SQL Server

时间:2019-02-04 10:37:12

标签: c# sql-server

我正在尝试使使用SQL Server的程序在另一台计算机上可用。我知道那台计算机将需要安装SQL Server客户端才能运行,但是我想知道我的ConnectionString是否可以在我自己的PC之外运行。

我需要将数据库附加到项目吗?如果是这样,我该怎么办?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\BENESQL;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123qwe;" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

3 个答案:

答案 0 :(得分:8)

您的数据源必须包含其他PC名称或IP地址,所以不要:

Data Source=.\BENESQL

您需要以下内容:

Data Source=123.123.123.123\BENESQL

此外,您还需要记住向防火墙等添加例外。

答案 1 :(得分:5)

您的连接字符串指向您的本地计算机,将其更改为:

connectionString="Data Source=<<server name or IP address>>\BENESQL;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123qwe;"

默认情况下,即使不是您的LAN,也不会将SQL Server设置为远程连接。在服务器上,您需要进入Sql Server配置管理器并启用TCP/IP

enter image description here

答案 2 :(得分:0)

不会,因为当前指向SQL Server的本地实例,所以不会。您可能还需要在连接字符串的Data Source属性中指定服务器IP和端口

Data Source=<IPAddress>\BENESQL;Initial Catalog=Northwind; 

有关更多信息,请参见https://www.connectionstrings.com/sql-server/

相关问题