无法仅从计算机上的应用程序连接到 SQL 服务器
本文关键字:连接 应用程序 SQL 服务器 计算机 | 更新日期: 2023-09-27 18:35:40
我有一个非常简单的应用程序:
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)
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
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
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource'1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at TestConsoleApplication1.Program.Main(String[] args) in p:''Projects''TestConsoleApplication1''TestConsoleApplication1''Program.cs:line 17
这个程序适用于其他人的机器。在我的机器上,在 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 从我的计算机进行连接
我的第一个想法是我的机器上的防火墙问题。不过我已经禁用了防火墙,但我仍然遇到问题。
所以问题是应用程序在P:驱动器上运行,这是一个网络驱动器。 通过 C: 驱动器复制内容,它可以工作。
这是一个有人遇到相同问题的问题:从 Win26 启动位于 NW 驱动器上的 C# exe 时出现"错误 26 - 定位服务器时出错"7;MSSQL