1

我有一个非常简单的应用程序:

    static void Main(string[] args)
    {
        try
        {
            var connection = new SqlConnection("Data Source=SQL;Initial Catalog=MyDatabase;Integrated Security=True");
            connection.Open();

            Console.WriteLine(connection.State.ToString());
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
        Console.ReadLine();
    }

运行应用程序给了我一个例外

建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)

内部异常:

访问被拒绝

堆栈跟踪

在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔 breakConnection,操作1 重试,DbConnectionOptions userOptions,DbConnectionInternal& 连接) 在 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource 1 重试,DbConnectionOptions userOptions) 在系统.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) at System.Data.SqlClient.SqlConnection.Open() at TestConsoleApplication1.Program.Main(String[] args) 在 p:\Projects\TestConsoleApplication1\TestConsoleApplication1\Program。 CS:第 17 行 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(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, 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)
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, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)
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.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource



这个应用程序适用于其他人的机器。并在我的机器上将以下内容输入到 c# 交互式作品中:

 Microsoft (R) Roslyn C# Compiler version 1.2.20906.1 
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.
  using System.Data.SqlClient;
  var connection = new SqlConnection("Data Source=SQL;Initial Catalog=MyDatabase;Integrated Security=True");
  connection.Open();
  connection.State
Open

如果连接是由在我的机器上运行的 WCF 服务建立的,那么连接似乎也可以工作。我可以使用 SQL Server Management Studio 和 Visual Studio Server Explorer 从我的机器进行连接

我的第一个想法是我机器上的防火墙问题。虽然我已经禁用了防火墙,但我仍然遇到问题。

4

1 回答 1

0

所以问题是应用程序在 P: 驱动器上运行,这是一个网络驱动器。通过 C: 驱动器复制内容,它可以工作。

这是一个有人遇到同样问题的问题:从 Win7 启动位于 NW 驱动器上的 C# exe 时出现“错误 26 - 错误定位服务器”;微软SQL

于 2013-10-15T17:43:17.160 回答