【问题标题】:Throw an error including the connection string抛出包含连接字符串的错误
【发布时间】:2017-12-19 19:17:36
【问题描述】:

我有一个使用 EF 连接到 SQL Server 数据库的 c# .NET Web 应用程序,它抛出“初始化字符串的格式不符合从索引 0 开始的规范”错误。这只发生在我的远程服务器上,它在本地运行良好。显然我的连接字符串不正确,我的问题不是直接与此错误有关。

我的应用程序有 5 个上下文匹配 5 个 SQL Server 数据库,每个上下文在我的 web.config 文件中都有一个连接字符串。我知道第一个上下文正在工作,但我不知道其他哪个上下文是问题所在。 web.config 中的 5 个连接字符串都非常相似,如果第一个(显然是工作的)工作正常,我看不出为什么有任何工作不工作。所有 5 个连接字符串过去都可以工作,现在发生了什么变化。

所以我想要更多的线索。

这是我 YSOD 的堆栈跟踪:

[ArgumentException: Format of the initialization string does not conform to specification starting at index 0.]
   System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) +1739
   System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) +191
   System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) +136
   System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +76
   System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +35
   System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +241
   System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) +78
   System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +116
   System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +931
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext) +509
   System.Data.Entity.Internal.LazyInternalConnection.Initialize() +77
   System.Data.Entity.Internal.LazyInternalConnection.get_Connection() +16
   System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver) +508
   System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) +370
   System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +140
   System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +72
   System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +483
   System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +177
   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +274
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +38
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +77
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
   System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues) +32
   Argus.RdAP.GetCodeFromIndex(Int32 IndexNumber) +140
(etc...)

好的,这是我的问题:如果我能看到错误的连接字符串是什么,那将非常方便。我非常清楚问题在代码中的哪里开始(即在 Argus.RdAP.GetCodeFromIndex 中)。是否有 try ... catch ... throw 一段代码,将在抛出的异常中包含实际的连接字符串?

对不起,如果这是一个愚蠢的问题。另外 - 欢迎访问连接字符串或修复我的潜在问题的替代建议。

【问题讨论】:

  • 我基本上会阻止将连接字符串的内容写入异常(即日志文件/服务器/...)连接字符串可能包含密码等机密信息。
  • 我的连接字符串没有密码,这是一个非常临时的措施,但很好,谢谢。

标签: c# sql-server entity-framework error-handling


【解决方案1】:

我终于找到了答案!

try
{
    // here was the method I suspected of causing the problem
}
catch
{
    throw new Exception(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2013-08-18
    • 2016-12-26
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多