gpt4 book ai didi

c# - 引发错误,包括连接字符串

转载 作者:行者123 更新时间:2023-12-03 07:53:26 25 4
gpt4 key购买 nike

我有一个使用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 ...抛出一段代码,该代码段将在抛出的异常中包括实际的连接字符串?

抱歉,这是一个愚蠢的问题。另外-欢迎访问连接字符串或解决我的基本问题的其他建议。

最佳答案

最终我找到了答案!

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

关于c# - 引发错误,包括连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45107186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com