且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何连接另一台机器的SQL数据库

更新时间:2023-02-02 17:02:19

只需从COM2打开SSMS并输入COM1 \SQLEXPRESS作为serverName



尝试连接它。如果您发现任何问题,请参阅

MSDN帮助 [ ^ ]


这是SQL Server你使用 SqlConnection类(System.Data.SqlClient) [ ^ ]建立与SQL Server的连接,该SQL Server可能驻留在您的计算机上或网络中的其他位置。该位置在 SqlConnection.ConnectionString属性中指定(System.Data.SqlClient) [ ^ ]并且它可能最容易使用 SqlConnectionStringBuilder类(System.Data.SqlClient) [ ^ ]构建连接字符串。



对于其他一些原则,请查看正确执行数据库操作 [ ^ ]


一个Sql server实例有为LAN / WAN上的每台机器可见。要实现这一点,您必须启用远程连接 [ ^ ] com1 machine。请参阅:

配置服务器以侦听特定TCP端口(SQL Server配置经理) [ ^ ]

配置服务器以在备用管道上侦听(SQL Server配置管理器) [ ^ ]



现在,您必须编写正确的连接字符串 [ ^ ]。



请注意,有时您必须配置Windows防火墙以允许SQL Server访问 [ ^ ]



祝你好运!

hello i have two computer com1 and com2 both this are developing the application with shared database. and the database is present in com1. i want to use the some database in com2. what actually i need to do thats why i can able to connect with com1.

What I have tried:

all this process i need to do using LAN or WAN network.
please help me

Just open SSMS from COM2 and key in "COM1\SQLEXPRESS" as serverName

Try connecting it. if you see any problems refer
MSDN HELP[^]


Taken this is SQL Server you use a SqlConnection Class (System.Data.SqlClient)[^] to establish a connection to a SQL Server which may reside on your computer or somewhere else in the network. The location is specified in SqlConnection.ConnectionString Property (System.Data.SqlClient)[^] and it's probably easiest to use SqlConnectionStringBuilder Class (System.Data.SqlClient)[^] to build the connection string.

For some further principles, have a look at Properly executing database operations[^]


An Sql server instance have to be "visible" for each machine on LAN/WAN. To achieve that you have to enable remote connections[^] for com1 machine. See:
Configure a Server to Listen on a Specific TCP Port (SQL Server Configuration Manager)[^]
Configure a Server to Listen on an Alternate Pipe (SQL Server Configuration Manager)[^]

Now, you have to write proper connection string[^].

Note that sometimes you have to Configure the Windows Firewall to Allow SQL Server Access[^]

Good luck!