[英]how to connect to remote SQL Server Express over internet?
I have a Windows 10 PC with SQL Server Express, I have setup remote connections, allocated a fixed port (express, 22172, removed TCP Dynamic Ports under IPALL), set an inbound rule for the port etc.我有一台 Windows 10 PC 和 SQL Server Express,我设置了远程连接,分配了一个固定端口(express,22172,删除了 IPALL 下的 TCP 动态端口),为端口设置入站规则等。
I can connect and manipulate the database from a Linux PC on the same.network (on the same LAN router), using:我可以从同一网络(同一 LAN 路由器)上的 Linux PC 连接和操作数据库,使用:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = @"192.168.1.22\SQLEXPRESS, 22172";
builder.InitialCatalog = "<database>";
builder.ItegratedSecurity= "sa";
builder.Password = "<password>"
string connectionString = builder.ConnectionString;
So, the normal firewall inbound rules, remote connection settings etc work, correct?那么,正常的防火墙入站规则、远程连接设置等是否有效,对吗? I now want to be able to make that same connection, from a Windows PC that is on a different inte.net (from a different location/town..)我现在希望能够从位于不同 inte.net(来自不同位置/城镇)的 Windows PC 建立相同的连接。
I have:我有:
I have switched off the firewall, and still get the error: "(provider: TCP Provider, error 40 - Could not open a connection to SQL Server)我已关闭防火墙,但仍然收到错误消息:“(提供商:TCP 提供商,错误 40 - 无法打开与 SQL 服务器的连接)
I try these connection string options:我尝试这些连接字符串选项:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = @"tcp:xxx.xxx.x.xx\SQLEXPRESS, 22172";
builder.InitialCatalog = "<database>";
builder.UserID = "sa";
builder.IntegratedSecurity = false;
builder.Password = "<password>";
sqlConnect = builder.ConnectionString;
I have also tried these variations:我也尝试过这些变化:
builder.DataSource = @"x.xxx.x.xx\SQLEXPRESS, 22172";
builder.DataSource = @"tcp:xxx.xxx.x.xx, 22172";
builder.DataSource = @"xxx.xxx.x.xx, 22172";
Also these connection strings:还有这些连接字符串:
sqlConnect = @"Data Source = tcp:xxx.xxx.x.xx\SQLEXPRESS,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
sqlConnect = @"Data Source = xxx.xxx.x.xx\SQLEXPRESS,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
sqlConnect = @"Data Source = tcp:xxx.xxx.x.xx,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
sqlConnect = @"Data Source = xxx.xxx.x.xx,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
And for good measure, this one:为了更好的衡量,这个:
sqlConnect = @"Data Source=xxx.xxx.x.xx\SQLEXPRESS,22172;Initial Catalog=<database>;Persist Security Info=True;User ID=sa;Password=<password>;";
I have scanned all relevant questions on SO etc - somewhere I am doing something small wrong?我已经扫描了所有关于 SO 等的相关问题 - 我在某个地方做错了什么? Please help请帮忙
Output when running select * from sys.tcp_endpoints
on host PC SQL: Thus, ip_address is NULL for both, port is 0 for both, and is_dynamic_port is 1 for both - is that the relevant information? Output 当select * from sys.tcp_endpoints
时:因此,两者的 ip_address 均为 NULL,两者的端口均为 0,两者的 is_dynamic_port 均为 1 - 这是相关信息吗?
Dedicated Admin Connection 1 1 2 TCP 2 TSQL 0 STARTED 1 0 1 NULL
TSQL Default TCP 4 1 2 TCP 2 TSQL 0 STARTED 0 0 1 NULL
I have tried several times to change the dynamic port to static, I have removed all 0 or port numbers in configuration manager, but still get a 1 for _is_dynamic_port.我曾多次尝试将动态端口更改为 static,我已删除配置管理器中的所有 0 或端口号,但 _is_dynamic_port 仍然为 1。 Event Viewer shows:事件查看器显示:
Server is listening on [ 'any' <ipv4> 22172].
Same message if I run the following in SQL:如果我在 SQL 中运行以下命令,则会出现相同的消息:
EXEC xp_ReadErrorLog 0, 1, N'Server is listening on', N'any', NULL, NULL, 'DESC'
GO GO
Why does SQL settings remain on dynamic ports?为什么 SQL 设置保留在动态端口上?
From your work, I suppose you set it all on the Win PC.根据您的工作,我想您将其全部设置在 Win PC 上。 So,...所以,...
SqlConnectionStringBuilder.IntegratedSecurity
.看看SqlConnectionStringBuilder.IntegratedSecurity
。 It should have a bool value.它应该有一个布尔值。 false
if user and password should be used for the authentication ( https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.integratedsecurity?view=dotnet-plat-ext-6.0 ).如果应使用用户名和密码进行身份验证,则为false
( https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.integratedsecurity?view=dotnet-plat-ext-6.0 ) .SqlConnectionStringBuilder.UserID
.看看SqlConnectionStringBuilder.UserID
。 It is missing in both of your code snippets.您的两个代码片段中都缺少它。 But mentioned at created connection strings Persist Security Info=True;User ID=sa;Password=<password>;
但在创建的连接字符串中提到Persist Security Info=True;User ID=sa;Password=<password>;
as «User ID», «user» и «uid».作为«用户ID»,«用户»和«uid»。 Please, see here ( https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.userid?view=dotnet-plat-ext-6.0 ).请参阅此处( https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.userid?view=dotnet-plat-ext-6.0 )。Check that your ISP does not block ports / all ports.检查您的 ISP 是否阻止端口/所有端口。 That is what stopped me.这就是阻止我的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.