我使用ORM来管理我的C#项目中的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 ---
我的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;
}
}
}
答案 0 :(得分:0)
可悲的是,无论OOP有多好,SQLite的WP实现并不总是按预期工作,通常你最好自己编写SQL查询。
话虽如此,您是否正确设置了您的房产?在您的数据库中,您有字段Owner
而不是OwnerId
,因此您需要将属性[Column(Name="Owner")]
分配给OwnerId
属性