2012-12-05 4 views
3

mysql 데이터베이스에서 일부 데이터를 가져 오기 위해 C# 코드 세그먼트를 작성하고 있습니다. 나는 준비가 만든 예제로 시작하지만, 그것은뿐만 아니라 내가이 떠올라 요C# mysql 예제 Command.ExecuteReader가 예외를 throw합니다

exception thrown screenshot

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). 
+0

예외는 무엇입니까? –

+0

at rdr = cmd.ExecuteReader() – Omran

+0

스택 추적과 같은 예외 세부 사항을 더 표시 할 수 있습니까? –

답변

4

아래와 같습니다 rdr = cmd.ExecuteReader()

에서 나에게 발생한 예외의 스크린 샷을 오류를 준 문제가 있었고 연구를 해봤습니다. 잠시 후, 나는이 문제를 해결했다.

문제점은 데이터베이스 테이블 인코딩입니다. 나는 utf32_unicode_ci을 가지고 있었고, 나는 utf8_unicode_ci으로 바 꾸었습니다. 이제 완벽하게 작동합니다.

당신은 그것에 행운이 있기를 바랍니다.

+0

어떻게 그럴 수 있습니까? –

관련 문제