2013-05-29 35 views
0

我想從我的SQLite數據庫中列出所有表,但我只需要普通表而不是系統表。我嘗試使用foreach,並在循環時刪除行,但我得到一個異常。任何人都可以請指出我在這裏做錯了什麼,因爲我真的不知道我要去哪裏錯了。無法從DataTable中刪除DataRow

我唯一的想法是,我無法從循環中刪除數據表,但我不知道如何處理這個問題,否則。

數據庫目前包含4個表,3個正常和1系統表

代碼:

 SQLiteConnection connection = new SQLiteConnection("Data Source=db.sqlite"); 

     connection.Open(); 

     DbTables = connection.GetSchema("Tables"); 

     foreach (DataRow row in DbTables.Rows) 
     { 
      Console.WriteLine(row[3].ToString()); 

      if (row[3].ToString() == "SYSTEM_TABLE") 
       DbTables.Rows.Remove(row); 
     } 

     connection.Close(); 

例外:

System.Windows.Data Error: 17 : Cannot get 'Main' value (type 'MainViewModel') from '' (type 'ViewModelLocator'). BindingExpression:Path=Main; DataItem='ViewModelLocator' (HashCode=65325907); target element is 'MainWindow' (Name=''); target property is 'DataContext' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Property accessor 'Main' on object 'DatabaseExplorer.ViewModel.ViewModelLocator' threw the following exception:'Exception has been thrown by the target of an invocation.' ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Collection was modified; enumeration operation might not execute. 
    at System.Data.RBTree`1.RBTreeEnumerator.MoveNext() 
    at DatabaseExplorer.ViewModel.MainViewModel..ctor(IDataService dataService) in Project\ViewModel\MainViewModel.cs:line 61 
    --- End of inner exception stack trace --- 
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 
    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) 
    at GalaSoft.MvvmLight.Ioc.SimpleIoc.MakeInstance[TClass]() 
    --- End of inner exception stack trace --- 
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 
    at System.Delegate.DynamicInvokeImpl(Object[] args) 
    at System.Delegate.DynamicInvoke(Object[] args) 
    at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type serviceType, String key) 
    at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetInstance[TService]() 
    at DatabaseExplorer.ViewModel.ViewModelLocator.get_Main() in Project\ViewModel\ViewModelLocator.cs:line 54 
    --- End of inner exception stack trace --- 
    at System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) 
    at MS.Internal.Data.ValueTable.GetValue(Object item, PropertyDescriptor pd, Boolean indexerIsNext) 
    at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level) 
    at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)' 

控制檯輸出時我刪除if子句:

SYSTEM_TABLE 
table 
table 
table 

提前致謝!

回答

3

枚舉時不能修改集合。因此,您可以將foreach循環更改爲for或創建臨時集合,該集合將填充要刪除的行並在之後將其刪除。

第一種方法(需要測試,我不是我的電腦前,現在):

for (int i = DbTables.Rows.Count; i >=0 ; i--) 
{ 
    if (DbTables.Rows[i].ToString() == "SYSTEM_TABLE") 
     DbTables.Rows.RemoveAt(i); 
} 

第二種方法:

DbTables = connection.GetSchema("Tables"); 

var rowsToBeRemoved = new List<DataRow>(); 

foreach (DataRow row in DbTables.Rows) 
{ 
    Console.WriteLine(row[3].ToString()); 

    if (row[3].ToString() == "SYSTEM_TABLE") 
     rowsToBeRemoved.Add(row); 
} 

foreach DataRow row in rowsToBeRemoved) 
{ 
    DbTables.Rows.Remove(row); 
} 
+0

呀你完全正確,右後我讀到這我記得我只需要表名,所以沒有必要刪除它們,我只需將普通表存儲在列表中。對不起,浪費您的時間,但非常感謝您的幫助!非常感謝! – Kryptoxx

+0

沒問題,總是樂意幫忙,甚至有點:) – gzaxx