SQLite 数据库连接方法
第一步:
第二步
第三步
namespace DBHelper.Base
{
public class DBHelperSQLite : DBHelperBase
{
const string FilePath = @"App_Data\Data.dat";
private static async Task<string> getFile()
{
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(FilePath);
return file.Path;
}
string Path { get; set; }
public string ConnectionString
{
get
{
if (Path == null || Path == string.Empty)
{
Path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path + "\\" + FilePath;
}
return Path;
}
}
#region Linq SQL
public override int Insert<T>(T Data)
{
int result = -1;
using (var db = new SQLite.SQLiteConnection(ConnectionString))
{
result = db.Insert(Data);
db.Close();
}
return result;
}
public override int Update<T>(T Data)
{
int result = -1;
using (var db = new SQLite.SQLiteConnection(ConnectionString))
{
db.Update(Data);
db.Close();
}
return result;
}
public override int Delete<T>(object primaryKey)
{
int result = -1;
using (var db = new SQLite.SQLiteConnection(ConnectionString))
{
db.Delete<T>(primaryKey);
db.Close();
}
return result;
}
public override List<T> Query<T>(string query, params object[] args)
{
List<T> result = null;
using (var db = new SQLite.SQLiteConnection(ConnectionString))
{
result = db.Query<T>(query, args);
db.Close();
}
return result;
}
#endregion
#region Linq Table
public int CreateTable<T>()
{
int result = -1;
using (var db = new SQLite.SQLiteConnection(ConnectionString))
{
result = db.CreateTable<T>();
db.Close();
}
return result;
}
public bool ExistTable<T>() where T : new()
{
bool result = false;
using (var db = new SQLite.SQLiteConnection(ConnectionString))
{
//result = db.Table<T>().;
db.Close();
}
return result;
}
public int DropTable<T>()
{
int result = -1;
using (var db = new SQLite.SQLiteConnection(ConnectionString))
{
result = db.DropTable<T>();
db.Close();
}
return result;
}
#endregion
}
}