C#的远程SQL Server的正确连接字符串 [英] The right connection string for Remote SQL server for C#

查看:354
本文介绍了C#的远程SQL Server的正确连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道远程sql server Express版的正确sql连接字符串。

I just want to know the right sql connection string for a remote sql server express edition.

这是我得到的,但是我遇到了一些问题

This is what I got but I got some problems

 SqlConnection cs = new SqlConnection(@"Data Source=(IP Address)\PC-NAME\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password");

我在C#调试器中遇到此错误:

I got this error in my C# debugger:

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.)

谢谢!

推荐答案

来自您的评论:


IP地址是静态IP地址。我正在尝试连接建筑物外部。

The IP address is the static IP address. I am trying to connect outside the building.

如果您要连接的数据库位于[router /具有目标地址的防火墙/调制解调器),您将需要在[路由器/防火墙/调制解调器]上使用端口转发,将TCP端口1433上的所有连接转发到目标计算机。

If the database you are trying to connect to is on a computer behind a [router/firewall/modem] that has the target address, you will need to use port forwarding on the [router/firewall/modem] to forward all connections on TCP port 1433 through to the target machine.

如果要连接到具有静态IP地址的ADSL调制解调器后面的家用计算机,则需要为您的ADSL调制解调器配置端口转发,以将外部端口1433连接到内部地址上的相同端口。 必须 必须在调制解调器上完成, 不能仅通过连接字符串完成。

If you are trying to connect to a home computer behind an ADSL modem with a static IP address, your ADSL modem needs to be configured with port forwarding to connect the external port 1433 with the same port on the internal address. This must be done on the modem, and cannot be done just with a connection string.

比方说,您有一个侦听127.2.3.4的ADSL调制解调器(仅用于演示的无效地址),其后有一台IP地址为192.168.0.100的PC。配置调制解调器以将端口1433转发到 192.168.0.100:1433 (具体取决于调制解调器的型号和型号)。然后您的连接字符串将是:

Let's say you have an ADSL modem listening on 127.2.3.4 (invalid address for demonstration only) and a PC behind that with an IP address of 192.168.0.100. Configure the modem to forward port 1433 to 192.168.0.100:1433 (how will depend on make and model of modem). Then your connection string would be:

Data Source=127.2.3.4\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password

假定 SQLEXPRESS 是服务器上唯一的数据库实例,由于默认端口为1433,因此可以使用以下更简单的方法:

Assuming that SQLEXPRESS is the only database instance on the server, and since port 1433 is the default, you could use the simpler:

Data Source=tcp:127.2.3.4;Initial Catalog=dbase;User ID=sa;Password=password

Network Library = DBMSSOCN 规范替换为 tcp:,并且假定实例和端口为默认值。

The Network Library=DBMSSOCN specification is replaced with tcp: and defaults are assumed for instance and port.

这篇关于C#的远程SQL Server的正确连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆