2014-09-22 28 views
0

我使用ORM來管理我的C#項目中的SQLite數據庫。這是它:https://github.com/praeclarum/sqlite-netsystem.reflection.targetinvocationexception SQLite

這真的希望但我不能使用SELECT Query。當我運行此:

var transacts = db.Table<Transact>(); 

我得到的回報此異常:system.reflection.targetinvocationexception在這條線:

public void SetValue(object obj, object val) 
{ 
    _prop.SetValue(obj, val, null); 
} 

完整的消息是: system.reflection.targetinvocationexception exception has been thrown by the target of an invocation

我有我的構造函數:

public Transact() 
    : base() 
{ 
    Console.WriteLine("yo"); 
} 

public Transact(int subCategoryIdT, string descriptionT, 
    DateTime dateT, double amountT, int ownerIdT) 
{ 
    db.CreateTable<Transact>(); 
    SubCategoryId = subCategoryIdT; 
    Description = descriptionT; 
    Date = dateT; 
    Amount = amountT; 
    OwnerId = ownerIdT; 
    db.Insert(this); 
    Console.WriteLine("I'm here !! I'm " + description + "."); 

} 

可以喲你看我的錯誤在哪裏?

設置InnerException的內容:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: Busy 
    at SQLite.SQLiteCommand.ExecuteNonQuery() [0x000c6] in /Users/gautier/Documents/Dev/csharp/bumget/bumget/SQLite.cs:2087 
    at SQLite.SQLiteConnection.Execute (System.String query, System.Object[] args) [0x00046] in /Users/gautier/Documents/Dev/csharp/bumget/bumget/SQLite.cs:627 
    at bumget.Transact.set_OwnerId (Int32 value) [0x0003a] in /Users/gautier/Documents/Dev/csharp/bumget/bumget/Transact.cs:49 
    at at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) 
    at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
    --- End of inner exception stack trace --- 

我的Transact類:

using System; 
using System.IO; 
using SQLite; 

namespace bumget 
{ 
    public class Transact 
    { 
     private int ownerId; 
     private int subCategoryId; 
     private string description; 
     private DateTime date; 
     private double amount; 
     private int expense; 

     private SQLiteConnection db = new SQLiteConnection (Path.Combine(Directory.GetCurrentDirectory(), "bumget.db3")); 

     public Transact() : base() { 
     } 

     public Transact (int subCategoryIdT,string descriptionT,DateTime dateT,double amountT,int ownerIdT, int expenseT) 
     { 
      db.CreateTable<Transact>(); 
      SubCategoryId = subCategoryIdT; 
      Description = descriptionT; 
      Date = dateT; 
      Amount = amountT; 
      OwnerId = ownerIdT; 
      Expense = expenseT; 
      db.Insert (this); 
      Console.WriteLine("I'm here !! I'm "+description+"."); 

     } 

     [PrimaryKey, AutoIncrement] 
     public int Id { 
      get; 
      private set; 
     } 

     public int OwnerId 
     { 
      get{ 
       return ownerId; 
      } 
      set{ 
       ownerId = value; 
       db.Execute("UPDATE Transact SET OwnerId = ? WHERE Id = ?",OwnerId,Id); 
      } 

     } 

     public int Expense 
     { 
      get{ 
       return expense; 
      } 
      set{ 
       expense = value; 
       db.Execute("UPDATE Transact SET Expense = ? WHERE Id = ?",Expense,Id); 
      } 

     } 

     public int SubCategoryId 
     { 
      get { 
       return subCategoryId; 
      } 
      set{ 
       subCategoryId = value; 
       db.Execute("UPDATE Transact SET SubCategoryId = ? WHERE Id = ?",SubCategoryId,Id); 
      } 
     } 

     public string Description 
     { 
      get{ 
       return description; 
      } 
      set{ 
       description = value; 
       db.Execute("UPDATE Transact SET Description = ? WHERE Id = ?",Description,Id); 
      } 
     } 

     public DateTime Date 
     { 
      get{ 
       return date; 
      } 
      set{ 
       date = value; 
       db.Execute("UPDATE Transact SET Date = ? WHERE Id = ?",Date,Id); 
      } 
     } 

     public double Amount 
     { 
      get{ 
       return amount; 
      } 
      set{ 
       amount = value; 
       db.Execute("UPDATE Transact SET Amount = ? WHERE Id = ?",Amount,Id); 
      } 
     } 

     public override string ToString() 
     { 
      return "Utilisateur :" + OwnerId + ", Montant: " + Amount + "CAN$, Date: " + Date.ToString() + ", Category : " + SubCategoryId + ", Description : " + Description + ", Owner = "+OwnerId + ", expense = "+Expense; 
     } 

    } 
} 
+1

異常的消息是什麼?你傳遞給_prop.SetValue方法的數據是什麼? – gunr2171 2014-09-22 16:48:37

+0

@ gunr2171我編輯了我的文章,並且傳遞了一個空對象+一個整數(這是我在打印obj和val時看到的內容) – Gautier 2014-09-22 17:07:13

+0

如果'obj'爲null,則不起作用。您無法將屬性值分配給空對象。 – gunr2171 2014-09-22 17:09:29

回答

0

可悲的是,無論OOP有多好時,SQLite的WP實現並不總是按預期工作,通常你最好自己寫SQL查詢。

話雖如此,你有沒有正確設置你的屬性?在您的數據庫中,您的字段爲Owner而不是OwnerId,因此您需要將屬性[Column(Name="Owner")]分配給您的OwnerId屬性

+0

感謝您的答案,但我有我的數據庫中的字段OwnerId,例如,此命令工作正常:'db.Execute(「UPDATE Transact SET OwnerId =?WHERE Id =?」,OwnerId,Id)' – Gautier 2014-09-23 02:20:42

+0

I很確定問題只是你的OwnerId屬性的映射。在上面的SELECT方法中,您寫道輸出爲: 用戶:0,Montant:0CAN $,日期:01/01/0001 00:00:00,類別:0,說明:,Owner = 0,費用=假 在這種情況下,我看不到一個OwnerId,只有一個Owner字段。但是,您知道,在回答基於SQL的問題時,查看C#數據模型和創建表的SQL查詢將非常方便。 – 2014-09-23 12:01:11

+0

'公共重寫字符串的ToString() \t \t { \t \t \t返回 「用戶:」 + OWNERID +」,Montant: 「+數額+ 」加元,日期:「 + Date.ToString()+」,分類: 「+ SubCategoryId +」,描述:「+ Description +」,Owner =「+ OwnerId +」,expense =「+費用; \t \t}'這就是爲什麼你看到所有者,而不是OwnerId ... – Gautier 2014-09-23 14:02:07

相關問題