我有一个简单的SQLite方法,它返回一个给定两个参数的类 - 类型T和要搜索的参数值
public T GetSingleObject<T>(string id) where T:IIdentity, new()
{
lock (dbLock)
{
using (var sqlCon = new SQLiteConnection(DBPath))
{
sqlCon.Execute(Constants.DBClauseSyncOff);
sqlCon.BeginTransaction();
string sql = string.Format("SELECT * FROM {0} WHERE id=\"{1}\"", GetName(typeof(T).ToString()), id);
var data = sqlCon.Query<T>(sql);
return data[0];
}
}
}
我还有另一个方法,它有3个参数,T,要坐的参数和要搜索的值
public T GetSingleObject<T>(string para, string val) where T:IIdentity, new()
{
lock (dbLock)
{
using (var sqlCon = new SQLiteConnection(DBPath))
{
sqlCon.Execute(Constants.DBClauseSyncOff);
sqlCon.BeginTransaction();
string sql = string.Format("SELECT * FROM {0} WHERE {1}=\"{2}\"", GetName(typeof(T).ToString()), para, val);
var data = sqlCon.Query<T>(sql).FirstOrDefault();
return data;
}
}
}
这些方法在没有问题的情况下工作并返回预期的值,但有一点需要注意 - 有一个表在手机有连接时工作正常,但在飞机模式下没有。该表中除了字符串,双精度,整数,DateTime和bool值之外没有其他内容。
GetName是一个简单的方法,可以删除程序集的位和部分,只留下类名
private string GetName(string name)
{
var list = name.Split('.').ToList();
if (list.Count == 1)
return list[0];
var last = list[list.Count - 1];
return last;
}
我已经尝试过各种方式来访问此表 - 包括直接
sqlCon.ExecuteScalar<MyClass>("SELECT * FROM MyClass");
并且没有离线,完美的在线数据。
该类本身看起来像这样
using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using SQLite;
namespace Models
{
public class MyClass : IIdentity
{
[PrimaryKey]
public string id { get; set; }
public string home_id { get; set; }
public string username { get; set; }
public string password { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public bool syncenabled { get; set; }
public string tradingname { get; set; }
public string address { get; set; }
public string town { get; set; }
public string state { get; set; }
public string country { get; set; }
public string securitystamp { get; set; }
public string password_question { get; set; }
public string password_answer { get; set; }
public string mobileServiceAuthenticationToken { get; set; }
public string providerUserKey { get; set; }
public string mobiledeviceid { get; set; }
public DateTime last_login { get; set; }
public DateTime __createdAt { get; set; }
public DateTime __updatedAt { get; set; }
public string user_type { get; set; }
public bool is_deleted { get; set; }
public bool account_enabled { get; set; }
public string subscription_id { get; set; }
[IgnoreDataMember, Ignore]
public List<MyUsers> UserModules { get { return AppDelegate.Self.DBManager.GetListOfObjects<MyUsers>("user_id", id); } }
public override string ToString()
{
return string.Format("[MyClasss: id={0}, home_id={1}, username={2}, password={3}, firstname={4}, lastname={5}, email={6}, syncenabled={7}, tradingname={8}, address={9}, town={10}, state={11}, country={12}, securitystamp={13}, password_question={14}, password_answer={15}, mobileServiceAuthenticationToken={16}, providerUserKey={17}, mobiledeviceid={18}, last_login={19}, __createdAt={20}, __updatedAt={21}, user_type={22}, is_deleted={23}, account_enabled={24}, subscription_id={25}, UserModules={26}]", id, home_id, username, password, firstname, lastname, email, syncenabled, tradingname, address, town, state, country, securitystamp, password_question, password_answer, mobileServiceAuthenticationToken, providerUserKey, mobiledeviceid, last_login, __createdAt, __updatedAt, user_type, is_deleted, account_enabled, subscription_id, UserModules);
}
}
我可以看到的任何内容都不会导致脱机时出现问题。查询始终在线下返回null,数据在线。