在Windows Phone上的c#中使用sqlite执行sql命令时出现异常

时间:2012-03-26 10:31:17

标签: sqlite

我的代码如下:

SqliteConnection db = new SqliteConnection("uri=file:zongheng.db");db.Open();
        SqliteCommand cmd = db.CreateCommand();
        cmd.CommandText = "update chapters set status = 0 where bookid=" + bookid + " and chapterid = " + reading_chapter + ";";
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        db.Close();
        db.Dispose();

        db.Open();
        cmd = db.CreateCommand();
        cmd.CommandText = ("select max(chapterid), status from chapters where status > 0 and bookid = "+bookid +";");
        SqliteDataReader dr = cmd.ExecuteReader();  //exception thrown from here

调用堆栈是这样的:

Community.CsharpSqlite.SQLiteClient.SqliteSyntaxException was unhandled
  Message=unable to open database file
  StackTrace:
       at Community.CsharpSqlite.SQLiteClient.SqliteCommand.GetNextStatement(String pzStart, String& pzTail, Vdbe& pStmt)
       at Community.CsharpSqlite.SQLiteClient.SqliteCommand.ExecuteReader(CommandBehavior behavior, Boolean want_results, Int32& rows_affected)
       at Community.CsharpSqlite.SQLiteClient.SqliteCommand.ExecuteReader(CommandBehavior behavior)
       at Community.CsharpSqlite.SQLiteClient.SqliteCommand.ExecuteReader()
       at ZonghengReader.Content.OnNavigatedTo(NavigationEventArgs e)
       at Microsoft.Phone.Controls.PhoneApplicationPage.InternalOnNavigatedTo(NavigationEventArgs e)
       at System.Windows.Navigation.NavigationService.RaiseNavigated(Object content, Uri uri, NavigationMode mode, Boolean isNavigationInitiator, PhoneApplicationPage existingContentPage, PhoneApplicationPage newContentPage)
       at System.Windows.Navigation.NavigationService.CompleteNavigation(DependencyObject content, NavigationMode mode)
       at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)
       at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
       at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at System.Delegate.DynamicInvokeOne(Object[] args)
       at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
       at System.Delegate.DynamicInvoke(Object[] args)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
       at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
       at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
       at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
       at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

如何在同一连接中执行不同的查询?任何建议?还是任何文件?任何建议都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

我不知道这个问题是否已经回答但是...在您的代码示例中处理了该对象:

...
db.Dispose();

db.Open();
...

老实说,我认为这是你目前背景下的问题。只需删除数据库连接对象上的Dispose()调用,我认为你应该没问题。一旦你真正完成了对象,你可以Dispose()它。