无法仅从我的计算机上的应用程序连接到SQL Server

时间:2013-10-15 17:05:57

标签: c# sql sql-server

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

    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的连接)

     

内部异常:

     

访问被拒绝

     

堆栈跟踪

     

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(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)
  在System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1重试,DbConnectionOptions userOptions)
  在System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1重试)
  在System.Data.SqlClient.SqlConnection.Open()
  在TestConsoleApplication1.Program.Main(String [] args)中的p:\ Projects \ TestConsoleApplication1 \ TestConsoleApplication1 \ Program.cs:第17行

此应用适用于其他人机器。在我的机器上输入以下内容到c#interactive works:

 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

从我的机器连接

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

1 个答案:

答案 0 :(得分:0)

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

以下是有人遇到同样问题的问题:‘error 26 - error locating server’ when launching C# exe located on NW drive from Win7; MSSQL