2010-03-20 1 views
1

저는 맞춤 SQLite 래퍼를 만들고 있습니다. 이는 데이터베이스에 대한 기존 연결을 허용하기위한 것입니다. 그러나이 함수를 두 번 호출 할 때 예외가 발생합니다.SQLiteDataReader를 사용하여 예외

public Boolean DatabaseConnected(string databasePath) 
{ 
    bool exists = false; 
    if (ConnectionOpen()) 
    { 
     this.Command.CommandText = string.Format(DATABASE_QUERY); 
     using (reader = this.Command.ExecuteReader()) 
     { 
      while (reader.Read()) 
      { 
       if (string.Compare(reader[FILE_NAME_COL_HEADER].ToString(), databasePath, true) == 0) 
       { 
        exists = true; 
        break; 
       } 
      } 
      reader.Close(); 
     } 
    } 
    return exists; 
} 

위의 기능을 사용하여 명령을 실행하거나 데이터베이스를 열기 전에 데이터베이스가 현재 열려 있는지 확인합니다. 처음으로 함수를 실행하면 문제없이 실행됩니다. 그 reader = this.Command.ExecuteReader() 예외를

Object reference not set to an instance of an object. 
StackTrace: 
    at System.Data.SQLite.SQLiteStatement.Dispose() 
    at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt) 
    at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt) 
    at System.Data.SQLite.SQLiteDataReader.NextResult() 
    at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) 
    at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) 
    at System.Data.SQLite.SQLiteCommand.ExecuteReader() 
    at EveTraderApi.Database.SQLDatabase.DatabaseConnected(String databasePath) in C:\Documents and Settings\galford13x\My Documents\Visual Studio 2008\Projects\EveTrader\EveTraderApi\Database\Database.cs:line 579 
    at EveTraderApi.Database.SQLDatabase.OpenSQLiteDB(String filename) in C:\Documents and Settings\galford13x\My Documents\Visual Studio 2008\Projects\EveTrader\EveTraderApi\Database\Database.cs:line 119 
    at EveTraderApiExample.Form1.CreateTableDataTypes() in C:\Documents and Settings\galford13x\My Documents\Visual Studio 2008\Projects\EveTrader\EveTraderApiExample\Form1.cs:line 89 
    at EveTraderApiExample.Form1.Button1_ExecuteCommand(Object sender, EventArgs e) in C:\Documents and Settings\galford13x\My Documents\Visual Studio 2008\Projects\EveTrader\EveTraderApiExample\Form1.cs:line 35 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
    at System.Windows.Forms.Button.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.Run(Form mainForm) 
    at EveTraderApiExample.Program.Main() in C:\Documents and Settings\galford13x\My Documents\Visual Studio 2008\Projects\EveTrader\EveTraderApiExample\Program.cs:line 18 
    at System.AppDomain._nExecuteAssembly(Assembly 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.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

답변

5

를 throw 한 후 연결을 관리 할 수있는 사용자 정의 래퍼를 가질 수 있지만,이 래퍼 명령을 관리해서는 안 : this.Command를. 명령 오브젝트가 랩퍼 내에 저장되므로 재사용됩니다. 문제는 처음으로 ExeuteReader으로 전화를 걸면이 명령이 삭제되고 두 번째 호출 할 때 예외가 발생한다는 것입니다. 각 쿼리에 대한 명령을 작성해보십시오.

public bool DatabaseConnected(string databasePath) 
{ 
    if (ConnectionOpen()) 
    { 
     using (var command = YourSQLiteConnection.CreateCommand()) 
     { 
      command.CommandText = string.Format(DATABASE_QUERY); 
      using (reader = command.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        if (string.Compare(reader[FILE_NAME_COL_HEADER].ToString(), databasePath, true) == 0) 
        { 
         return true; 
        } 
       } 
      } 
     } 
    } 
    return false; 
} 
+0

감사합니다. – galford13x