我只想知道远程 sql server express 版本的正确 sql 连接字符串。
这是我得到的,但我遇到了一些问题
SqlConnection cs = new SqlConnection(@"Data Source=(IP Address)\PC-NAME\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password");
我在 C# 调试器中遇到了这个错误:
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: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
提前致谢!
最佳答案
来自您的评论:
The IP address is the static IP address. I am trying to connect outside the building.
如果您尝试连接的数据库位于具有目标地址的 [路由器/防火墙/调制解调器] 后面的计算机上,您将需要在 [路由器/防火墙/调制解调器] 上使用端口转发来转发所有通过 TCP 端口 1433 连接到目标机器。
如果您尝试连接到具有静态 IP 地址的 ADSL 调制解调器后面的家庭计算机,您的 ADSL 调制解调器需要配置端口转发,以连接外部端口 1433 与内部地址上的相同端口。这必须在调制解调器上完成,不能只用连接字符串完成。
假设您有一个 ADSL 调制解调器监听 127.2.3.4(无效地址仅供演示),其后的 PC 的 IP 地址为 192.168.0.100。配置调制解调器以将端口 1433 转发到 192.168.0.100:1433
(如何取决于调制解调器的品牌和型号)。那么您的连接字符串将是:
Data Source=127.2.3.4\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password
假设 SQLEXPRESS
是服务器上唯一的数据库实例,并且由于端口 1433 是默认端口,您可以使用更简单的方法:
Data Source=tcp:127.2.3.4;Initial Catalog=dbase;User ID=sa;Password=password
Network Library=DBMSSOCN
规范被替换为 tcp:
并且假定实例和端口为默认值。
关于c# - Remote SQL server for C# 的正确连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20558043/