C# C mysql示例命令。ExecuteReader引发异常
我正在编写一段C代码,从mysql数据库中获取一些数据。我从现成的示例开始,但它在rdr=cmd.ExecuteReader处给了我一个错误 下面还显示了抛出异常的屏幕截图C# C mysql示例命令。ExecuteReader引发异常,c#,mysql,C#,Mysql,我正在编写一段C代码,从mysql数据库中获取一些数据。我从现成的示例开始,但它在rdr=cmd.ExecuteReader处给了我一个错误 下面还显示了抛出异常的屏幕截图 我一直有这个问题,我做了一些研究。过了一会儿,我就解决了这个问题 我的问题是数据库表编码。我有utf32\uUnicode\uCI,我把它改成了utf8\uUnicode\uCI。它现在工作得很好 希望你有点运气。抛出的异常在哪一行?在rdr=cmd.executeReader,你能显示更多的异常详细信息吗,比如堆栈跟踪?
我一直有这个问题,我做了一些研究。过了一会儿,我就解决了这个问题 我的问题是数据库表编码。我有utf32\uUnicode\uCI,我把它改成了utf8\uUnicode\uCI。它现在工作得很好
希望你有点运气。抛出的异常在哪一行?在rdr=cmd.executeReader,你能显示更多的异常详细信息吗,比如堆栈跟踪?我编辑了这篇文章以显示堆栈跟踪。这是一篇类似的文章[此处][1]。你可能会得到解决方案。[1]:
using System;
using MySql.Data.MySqlClient;
public class Program
{
static void Main()
{
string serverName = "192.168.0.012";
string port = "1234";
string db = "cust_db";
string userID = "root";
string password = "pass";
string cs = "Server=" + serverName + ";Port=" + port + ";Database=" + db + ";Uid=" + userID + ";password=" + password;
MySqlConnection conn = null;
MySqlDataReader rdr = null;
try
{
conn = new MySqlConnection(cs);
conn.Open();
string stm = "SELECT * FROM cust_tb";
MySqlCommand cmd = new MySqlCommand(stm, conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr.GetInt32(0) + ": "
+ rdr.GetString(1));
}
} catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
} finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}
at MySql.Data.MySqlClient.CharSetMap.GetChararcterSet(DBVersion version, String CharSetName)
at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData41()
at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData()
at MySql.Data.MySqlClient.NativeDriver.ReadColumnMetadata(Int32 count)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at Program.Main() in c:\Users\omran.alhammadi\Desktop\csMysqlConnection\csMysqlConnection\Program.cs:line 28
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at MySql.Data.MySqlClient.CharSetMap.GetChararcterSet(DBVersion version, String CharSetName)
at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData41()
at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData()
at MySql.Data.MySqlClient.NativeDriver.ReadColumnMetadata(Int32 count)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at Program.Main() in c:\Users\omran.alhammadi\Desktop\csMysqlConnection\csMysqlConnection\Program.cs:line 28
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
Additional information: The given key was not present in the dictionary.
The program '[17084] csMysqlConnection.vshost.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff).