如果从网络路径执行 EXE,则 SqlConnection 错误

2021-09-16 00:00:00 windows-10 vb.net sql-server

首先:您在这篇文章中读到的所有内容仅在安装了 Windows 10 April 2018 更新的情况下发生.安装前和更新卸载后都没有问题.

First of all: everything you will read in this post happens only if Windows 10 April 2018 update is installed. No problem before installation and after update uninstallation.

安装 Windows 10 1803 更新后,我所有从网络映射驱动器或 UNC 路径运行的 VB 程序(VB6、.NET 和 WPF)都无法连接到 SQL 服务器,如果在本地放置并执行相同的可执行文件,则没问题驱动器(在同一网络中的 2 台电脑上测试):

After installing Windows 10 1803 update, all my VB program (VB6, .NET and WPF) running from network mapped drive or UNC path can't connect to SQL server, no problem if the same executable is placed and executed from local drive (tested on 2 pc in the same network):

  1. 远程 SQL 服务器,本地驱动器上的 exe:OK
  2. 相同远程 SQL 服务器,映射网络驱动器上的 相同 exe(具有完全读/写访问权限):错误
  1. Remote SQL server, exe on local drive: OK
  2. Same remote SQL server, same exe on mapped network drive (with full read/write access): ERROR

这是错误(可能对解决此问题没有意义):

This is the error (maybe not significat to solve this problem):

与 SQL Server 建立连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接.(提供程序:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例).

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified).

简单的VB.NET代码复现问题(将代码放在一个简单的形式,在button_click事件中加一个按钮,设置值连接到SQL服务器,编译,将exe文件保存在网络路径上并执行)):

Simple VB.NET code to reproduce the problem (place the code in a simple form with a button in the button_click event, set values to connect to the SQL server, compile, save the exe file on a network path and execute it):

Dim myConnectionString As String
Dim mySqlConnectionStringBuilder As New SqlConnectionStringBuilder()
mySqlConnectionStringBuilder.DataSource = myServer
mySqlConnectionStringBuilder.InitialCatalog = myDatabase
mySqlConnectionStringBuilder.UserID = myUtente
mySqlConnectionStringBuilder.Password = myPassword
myConnectionString = mySqlConnectionStringBuilder.ConnectionString

Dim mySqlConnection As New SqlConnection(myConnectionString)
mySqlConnection.Open()  <- error

异常:

System.Data.SqlClient.SqlException (0x80131904): Si è verificato un errore di rete o specifico dell'istanza mentre si cercava di stabilire una connessione con SQL Server. Il server non è stato trovato o non è accessibile. Verificare che il nome dell'istanza sia corretto e che SQL Server sia configurato in modo da consentire connessioni remote. (provider: SQL Network Interfaces, error: 26 - Errore nell'individuazione del server/dell'istanza specificata)
   in System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
   in System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   in System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   in System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   in System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   in System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   in System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   in System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   in System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   in System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   in System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   in System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   in System.Data.SqlClient.SqlConnection.Open()
   in RiepilogoOreTimer.RiepilogoOreTimerWindow.ConnessioneOK()
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:-1,State:0,Class:20

有什么想法吗?

更新:如果我卸载 2018 年 4 月的更新,问题就会消失,即使在网络驱动器上执行该程序也能正常工作,但这不能成为解决方案...

Update: If I uninstall the April 2018 update the issue go away and the program works fine even if executed on a network drive, but this can't be the solution...

2018 年 8 月 5 日更新:我注意到 2018 年 4 月的更新带来了一些 安全变化:

Update 08/05/2018: I noticed that April 2018 update brought some changes in security:

Windows 10 版本 1803 提供额外保护:

Windows 10, version 1803 provides additional protections:

  • 新的攻击面减少规则
  • 受控文件夹访问现在可以阻止磁盘扇区

这可能是问题的原因吗?我不是安全经理,所以我不能说这是否会导致我的问题

Could it be the cause of the issue? I'm not a security manager so I can't say if this can cause my problem

2018 年 9 月 5 日更新:我在 this 帖子:

Windows 10 更新 1803 无法打开网络连接SMBv1 共享上的可执行文件(如 Windows Server 2003)

Windows 10 update 1803 does not open network connections on executables files on SMBv1 share (as Windows Server 2003)

但我不知道什么是 SMBv1...有人可以帮助我吗?

but I don't know what SMBv1 is... somebody can help me?

推荐答案

终于发现问题了:在共享文件夹的服务器中,SMBv2被禁用(不知道为什么)所以只有SMBv1处于活动状态;从同一网络中的同一客户端执行的同一程序但位于启用了 SMBv2 的服务器上运行正常.

Finally I found the problem: in the server with the shared folder, SMBv2 is disabled (I don't know why) so only SMBv1 is active; the same program executed from the same client in the same network but located on a server with SMBv2 enabled works fine.

所以问题是关于 SMBv1 共享,从 Windows 10 1803 开始​​弃用

So the problem is about SMBv1 share, deprecated starting from Windows 10 1803

相关文章