我使用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;
}
}
}
异常的消息是什么?你传递给_prop.SetValue方法的数据是什么? – gunr2171 2014-09-22 16:48:37
@ gunr2171我编辑了我的文章,并且传递了一个空对象+一个整数(这是我在打印obj和val时看到的内容) – Gautier 2014-09-22 17:07:13
如果'obj'为null,则不起作用。您无法将属性值分配给空对象。 – gunr2171 2014-09-22 17:09:29