【发布时间】: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 的内容进行了编辑