为什么SqlConnection.Open从我的一个网络驱动器发生故障?

时间:2015-01-05 16:09:07

标签: .net sql-server security database-connection

我有一个非常简单的.Net 4.5 exe(下面的完整源代码),它只对网络SQL Server执行SqlConnection.Open,但是如果它是从我的一个网络驱动器启动的,则它会因SqlException而失败&#34; < em>建立与SQL Server的连接时发生与网络相关或特定于实例的错误。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。&#34; (以下完整的例外情况)。当服务器和/或实例名称拼写错误时,我常常看到此错误,但事实并非如此 - 名称正确且exe从我的本地C:驱动器和某些映射的网络驱动器成功运行,但是只是不是其中之一。连接到本地SQL Server实例可以正常工作。

我尝试过多个联网SQL Server,行为一致。如前所述,连接到本地SQL Server实例可以正常工作。

我猜这可能在某种程度上与CAS政策有关,但是从我用Google搜索的内容来看,在.Net 4中删除了从网络驱动器加载代码的限制。

首先,我想知道并了解发生了什么。其次,我想在应用程序中编写测试代码,这样如果它以这种方式失败,它就会退出并显示有用的错误消息。

任何帮助都非常感激。

里斯。

Update1 :如果我将不起作用的UNC路径映射到新的驱动器号,它仍然无法正常工作。在连接字符串中使用SQL Server别名或IP地址没有任何区别。

Update2 :在客户端PC和SQL Server服务器上具有本地管理员权限的用户帐户下尝试此操作。这表明潜在的问题是一个安全问题,但对于(客户端或服务器)或缺少权限的位置没有帮助。


完整资源

using System;
using System.Data.SqlClient;

namespace SqlConnOpenTest
{
    class Program
    {
    static void Main()
    {
        Console.WriteLine("Started.");

        try
        {
        SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder
        {
            DataSource = "MyServer\\MyInstance",
            InitialCatalog = "master",
            IntegratedSecurity = true
        };

        string connectionString = csb.ConnectionString;

        Console.WriteLine("Connection string is; \"{0}\".", csb);

        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            sqlConnection.Open();
            Console.WriteLine("Connection opened successfully.");
            sqlConnection.Close();
        }

        }
        catch (Exception ex)
        {
        Console.Error.WriteLine(ex.ToString());
        }

        Console.WriteLine("Finished, press Enter to exit.");
        Console.ReadLine();
    }
    }
}

完全例外

System.Data.SqlClient.SqlException (0x80131904): 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)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfoserverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfoserverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at SqlConnOpenTest.Program.Main()
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:-1,State:0,Class:20

0 个答案:

没有答案