【问题标题】:Can't connect a console app to SQLExpress [duplicate]无法将控制台应用程序连接到 SQLExpress [重复]
【发布时间】:2021-10-29 19:58:05
【问题描述】:

我知道有很多类似的问题。最近几天,我花时间浏览了问题的答案并尝试了所有建议,这是建议的。但是没有任何效果,这就是我提出问题的原因,希望有人有想法。

我有一个控制台应用程序和一个 Web 应用程序,它们都在同一台服务器上运行。它们都连接到同样位于同一服务器上的 SQLExpress 数据库。在 IIS 上运行的 Web 应用程序能够连接到数据库并可以读取和写入数据。但是控制台应用程序不能。 这是我收到的错误消息:

System.Data.SqlClient.SqlException (0x80131904):网络相关或 建立连接时发生特定于实例的错误 SQL 服务器。服务器未找到或无法访问。核实 实例名称正确且 SQL Server 配置为 允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接) System.ComponentModel.Win32Exception (0x80004005): 网络路径 没找到

bei 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)
   bei System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   bei System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   bei System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   bei System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   bei System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   bei System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   bei System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   bei System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   bei System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   bei System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   bei System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   bei System.Data.SqlClient.SqlConnection.Open()
   bei System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<>c.<Open>b__13_0(DbConnection t, DbConnectionInterceptionContext c)
   bei System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   bei System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   bei System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass60_0.<UsingConnection>b__0()
   bei System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass2_0.<Execute>b__0()
   bei System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   bei System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   bei System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act)
   bei System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act)
   bei System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)
   bei System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   bei System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   bei System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
   bei System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
   bei System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   bei System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   bei System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext)
   bei System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState)
   bei System.Data.Entity.Database.Create(DatabaseExistenceState existenceState)
   bei System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
   bei System.Data.Entity.Internal.InternalContext.<>c__DisplayClass66_0`1.<CreateInitializationAction>b__0()
   bei System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
   bei System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   bei System.Data.Entity.Internal.LazyInternalContext.<>c.<InitializeDatabase>b__58_0(InternalContext c)
   bei System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
   bei System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
   bei System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
   bei System.Data.Entity.Internal.InternalContext.Initialize()
   bei System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   bei System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   bei System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   bei System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   bei System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)

ClientConnectionId:00000000-0000-0000-0000-000000000000 错误 编号:53,状态:0,等级:20

两个项目的连接字符串相同:

<connectionStrings>
    <add name="DbContextModel" connectionString="data source=DESKTOP\SQLEXPRESS;initial catalog=ApplicationDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>

我已经尝试并检查了很多东西,包括:

  • 检查所有 SQL 服务是否正在运行
  • 命名管道,TCP 在 SQL 配置管理器中激活
  • 我尝试关闭防病毒和防火墙 -> 没有区别
  • 我将User作为用户添加到数据库的Login中,在控制台应用启动时使用
  • 我尝试更改连接字符串
  • 我在 SQL 配置管理器中检查并插入了 tcp 端口 1433
  • 我检查了 SQL 是否允许远程连接
  • 我检查并确保 SQL 在 tcp 情况下使用端口 1433

我现在不知道接下来应该做什么或尝试下一步。而且我不明白为什么Web应用程序使用数据库没有问题,而控制台应用程序却不能。所以我真的很感激任何帮助。 非常感谢。

【问题讨论】:

  • 更改“源=DESKTOP\SQLEXPRESS;”到 "source=.\SQLEXPRESS;"
  • 控制台应用的App.config中是否有连接字符串?
  • 更改“source=DESKTOP\SQLEXPRESS;”到“源=.\SQLEXPRESS;”没有解决问题。
  • 是的,连接字符串在控制台的 App.config 中
  • 当你消除了不可能的事情后,剩下的无论多么不可能,都必须是事实。仔细检查 Web 应用程序的配置(在 IIS 本身中,不要简单地依赖本地配置文件)以查看它使用的确切连接字符串有效(如果你负担得起,重新启动它以确认它仍然可以运行),然后加倍检查(使用断点或日志记录)控制台配置中的连接字符串是否是正在使用的实际字符串(一种廉价的方法是使字符串无效,例如xxx,如果 barfs 在同一点查看它)。

标签: asp.net sql-server entity-framework


【解决方案1】:

我得到提示来仔细检查连接字符串,以及我的应用程序是否真的使用了配置文件的连接字符串。所以我仔细检查并注意到通过在配置文件中写入无效的连接字符串,错误消息没有改变。 原因是,无论出于何种原因,bin/Debug 中都有另一个名为 Projectname.config 的配置文件。我猜 Visual Studio 把它放在那里。应用程序正在从该配置文件中读取较旧的连接字符串,这导致了问题。

【讨论】:

    猜你喜欢
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多