连接到SQL Server Express时连接字符串无效

时间:2021-11-20 07:44:14

I tried every single variation that I could find on the internet. Everything. And still every time I get the very same exception:

我尝试了在互联网上找到的每一个变体。一切。而且每当我得到同样的异常时:

System.Data.SqlClient.SqlException: '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: 25 - Connection string is not valid)'

System.Data.SqlClient.SqlException:'与SQL Server建立连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:TCP提供者,错误:25 - 连接字符串无效)'

With this weird inner exception:

有了这个奇怪的内部异常:

SocketException: The operation completed successfully

SocketException:操作成功完成

When I specify the port directly, then I end up with this wonderful exception:

当我直接指定端口时,我最终得到了这个奇妙的异常:

An attempt was made to access a socket in a way forbidden by its access permissions

尝试以其访问权限禁止的方式访问套接字

No matter what, it doesn't work. Moreover I'd like to have MS SQL Management Studio open while testing (that was the only process which could block the port).

无论如何,它都行不通。此外,我想在测试时打开MS SQL Management Studio(这是唯一可以阻止端口的进程)。

So why does it not work? Management Studio can connect with IP\InstanceName without a problem. Why is the System.Data.Sql.SqlConnection class incapable of doing so?

那为什么它不起作用? Management Studio可以毫无问题地连接IP \ InstanceName。为什么System.Data.Sql.SqlConnection类无法执行此操作?

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = "tcp:***.***.***.***\\SQLEXPRESS";
        builder.InitialCatalog = "MyDB";
        builder.Password = "MyPass";
        builder.UserID = "ID";
        builder.IntegratedSecurity = false;
        _connection = new SqlConnection(builder.ToString());

FYI: I am using the ConnectionStringBuilder, so there shouldn't be any issue. Server name, instance name, SQL user and password are valid and I did explicitly set Integrated Security = False.

仅供参考:我使用的是ConnectionStringBuilder,所以应该没有任何问题。服务器名称,实例名称,SQL用户和密码有效,我明确设置了Integrated Security = False。

2 个解决方案

#1


1  

The problem was (which was not really clear) that UWP requires a capabilitiy in order to run that code. I read somewhere that you would need Enterprise Authentication which was not the one. Since my program and server are in a closed, private network I enabled Private Networks (Client & Server).

问题是(这不是很清楚)UWP需要一个能力才能运行该代码。我在某处读到你需要企业身份验证而不是那个。由于我的程序和服务器位于封闭的专用网络中,因此我启用了专用网络(客户端和服务器)。

The one you would need is Internet (Client & Server). Sadly the exceptions don't hint to that and the fact that SqlClient on UWP requires this to work is nearly nowhere mentioned or not stressed enough so you wouldn't overlook it.

您需要的是Internet(客户端和服务器)。遗憾的是,异常并没有暗示这一点,并且UWP上的SqlClient要求它工作的事实几乎没有提到或没有足够的压力,所以你不会忽略它。

Solution found in this .Net Standard issue.

此.Net标准问题中找到的解决方案。

#2


0  

First of all try to connect to this database using SQL Server Management Studio. If you get the same error, it means that there is a problem with the server not (or not only) with your connection string. Here is some of possibilities :

首先尝试使用SQL Server Management Studio连接到此数据库。如果您收到相同的错误,则表示服务器没有(或不仅仅)与您的连接字符串存在问题。以下是一些可能性:

1- Remove 'tcp:' from this part: builder.DataSource = "tcp:...\SQLEXPRESS";

1-从此部分删除'tcp:':builder.DataSource =“tcp:... \ SQLEXPRESS”;

2- On the server the SQL Server services (SQL Server or SQL Server browser) are stopped. To activate go Services and start stopped services.

2-在服务器上停止SQL Server服务(SQL Server或SQL Server浏览器)。激活go服务并启动已停止的服务。

3- May be the server doesn't accept remote connections: In this case on the server open SQL Server Configuration Manager

3-可能是服务器不接受远程连接:在这种情况下,在服务器上打开SQL Server配置管理器

连接到SQL Server Express时连接字符串无效

And activate tcp ip.

并激活tcp ip。

You should restart SQL Server service after this operation.

您应该在此操作后重新启动SQL Server服务。

Tell me if it works for you.

告诉我它是否适合你。

#1


1  

The problem was (which was not really clear) that UWP requires a capabilitiy in order to run that code. I read somewhere that you would need Enterprise Authentication which was not the one. Since my program and server are in a closed, private network I enabled Private Networks (Client & Server).

问题是(这不是很清楚)UWP需要一个能力才能运行该代码。我在某处读到你需要企业身份验证而不是那个。由于我的程序和服务器位于封闭的专用网络中,因此我启用了专用网络(客户端和服务器)。

The one you would need is Internet (Client & Server). Sadly the exceptions don't hint to that and the fact that SqlClient on UWP requires this to work is nearly nowhere mentioned or not stressed enough so you wouldn't overlook it.

您需要的是Internet(客户端和服务器)。遗憾的是,异常并没有暗示这一点,并且UWP上的SqlClient要求它工作的事实几乎没有提到或没有足够的压力,所以你不会忽略它。

Solution found in this .Net Standard issue.

此.Net标准问题中找到的解决方案。

#2


0  

First of all try to connect to this database using SQL Server Management Studio. If you get the same error, it means that there is a problem with the server not (or not only) with your connection string. Here is some of possibilities :

首先尝试使用SQL Server Management Studio连接到此数据库。如果您收到相同的错误,则表示服务器没有(或不仅仅)与您的连接字符串存在问题。以下是一些可能性:

1- Remove 'tcp:' from this part: builder.DataSource = "tcp:...\SQLEXPRESS";

1-从此部分删除'tcp:':builder.DataSource =“tcp:... \ SQLEXPRESS”;

2- On the server the SQL Server services (SQL Server or SQL Server browser) are stopped. To activate go Services and start stopped services.

2-在服务器上停止SQL Server服务(SQL Server或SQL Server浏览器)。激活go服务并启动已停止的服务。

3- May be the server doesn't accept remote connections: In this case on the server open SQL Server Configuration Manager

3-可能是服务器不接受远程连接:在这种情况下,在服务器上打开SQL Server配置管理器

连接到SQL Server Express时连接字符串无效

And activate tcp ip.

并激活tcp ip。

You should restart SQL Server service after this operation.

您应该在此操作后重新启动SQL Server服务。

Tell me if it works for you.

告诉我它是否适合你。