system.reflection.targetinvocationexception SQLite

Posted

技术标签:

【中文标题】system.reflection.targetinvocationexception SQLite【英文标题】: 【发布时间】:2014-09-22 16:45:26 【问题描述】:

我在我的 C# 项目中使用 ORM 来管理我的 SQLite 数据库。就是这样:https://github.com/praeclarum/sqlite-net

真的很有希望,但我不能使用 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 ---

我的交易类:

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;
        

    

【问题讨论】:

异常的信息是什么?你向_prop.SetValue 方法传递了什么数据? @gunr2171 我已经编辑了我的帖子,我正在传递一个空对象 + 一个整数(这是我在打印 obj 和 val 时看到的) 如果obj 为空,这将不起作用。您不能将属性值分配给空对象。 obj is not null 实际上我得到了这个:User :0, Montant: 0CAN$, Date: 01/01/0001 00:00:00, Category : 0, Description : , Owner = 0, expense = False 而不是 User :12, Montant: 12,06CAN$, Date: 22/09/2014 13:05:03, Category : 1, Description : Beers, Owner = 12, expense = True 我已经用 innerException 的内容进行了编辑 【参考方案1】:

遗憾的是,无论 OOP 多么出色,SQLite 的 WP 实现并不总是按预期工作,通常你最好自己编写 SQL 查询。

话虽如此,您是否正确设置了属性?在您的数据库中,您有字段Owner 而不是OwnerId,因此您需要将属性[Column(Name="Owner")] 分配给您的OwnerId 属性

【讨论】:

感谢您的回答,但我的数据库中有 OwnerId 字段,例如此命令可以正常工作:db.Execute("UPDATE Transact SET OwnerId = ? WHERE Id = ?",OwnerId,Id) 我很确定问题只是您的 OwnerId 属性的映射。在上面的 SELECT 方法中,您写道输出为:用户:0,Montant:0CAN$,日期:01/01/0001 00:00:00,类别:0,描述:,所有者 = 0,费用 = 假在这种情况下,我看不到 OwnerId,只有 Owner 字段。但是,您知道,在回答基于 SQL 的问题时,查看您的 C# 数据模型和创建表的 SQL 查询会非常方便。 public override string ToString() return "User :" + OwnerId + ", Montant: " + Amount + "CAN$, Date: " + Date.ToString () + ", Category : " + SubCategoryId + ", Description : " + Description + ", Owner = "+OwnerId + ", expense = "+Expense; 这就是为什么你看到 Owner 而不是 OwnerId... 我明白了...好吧,我说的仍然成立:您的异常显然在路径中显示了 set_OwnerId,因此在此之前它没有问题。你能用你的整个 Transact 类和数据库中的 CREATE TABLE 查询来更新你的主帖子吗? 完成。但是表是由 ORM 自动创建的。感谢您的帮助。

以上是关于system.reflection.targetinvocationexception SQLite的主要内容,如果未能解决你的问题,请参考以下文章