【发布时间】:2020-01-31 03:57:27
【问题描述】:
我正在尝试运行这行代码以使用 csharp 和 dotnet core 2.1.300 (sdk) 连接到 mysql。
string server = "localhost";
string database = "db";
string uid = "root";
string password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
connection.Open();
但是最后一行会引发一个异常,我无法理解解决它的问题所在。 使用 dotnet 运行项目的 dll 向我展示了这一点:
dotnet :
At line:1 char:1
+ dotnet C:\Users\foo\bar\project ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Unhandled Exception:
Cannot print exception string because Exception.ToString() failed.
我尝试通过 similar question 之类的帖子进行推荐,并将 System.Runtime.Serialization.Primitives 程序集添加到我在 VS 2017 中的引用中,但没有帮助。
我实际上正在修改这个 csharp 项目,以便将其输出写入数据库。 https://github.com/vcsjones/AuthenticodeLint
将 MySqlException 更改为 Exception 将导致以下输出:
Unhandled Exception:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr*
methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[]
typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly&
lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean
mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean&
ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken,
RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.Diagnostics.StackTrace.ShowInStackTrace(MethodBase mb)
at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
....
【问题讨论】:
-
它在
connection.Open();行吗?我们可以获取完整的堆栈跟踪吗? -
@Çöđěxěŕ:我无法打印堆栈跟踪。(或者我不知道该怎么做!根据我的理解,虽然我捕捉到最后一行引发的异常,但打印堆栈跟踪或异常消息引发另一个异常并失败!)
-
我正在处理这样的异常:try { connection.Open(); } catch (MySqlException ex) { verboseWriter.LogMessage(ex.StackTrace);即使我注释掉这一行 verboseWriter.LogMessage(ex.StackTrace);我仍然收到“未处理的异常”错误。
-
将您的例外更改为
Exception而不是MySqlException。当你在那里放一个断点然后检查Message时你会得到什么...如果这不起作用你是否至少尝试过2.2版;如果我没记错的话,这是 2.1x 中的一个问题... -
我按照你的要求更新了我的帖子(更改为例外)。
标签: c# mysql exception visual-studio-2017