コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using SQLite;
public interface ISQLService
{
SQLiteConnection GetConnection();
}
using SQLite;
[assembly: Dependency(typeof(SQLService))]
public class SQLService : ISQLService
{
public SQLiteConnection GetConnection()
{
var personalPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = System.IO.Path.Combine(personalPath, "MyDatabaseName");
return new SQLiteConnection(path);
}
}
using SQLite;
[assembly: Dependency(typeof(SQLService))]
public class SQLService : ISQLService
{
public SQLiteConnection GetConnection()
{
var personalPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var libraryPath = System.IO.Path.Combine(personalPath, "..", "Library");
var path = System.IO.Path.Combine(libraryPath, "MyDatabaseName");
return new SQLiteConnection(path);
}
}
using SQLite;
public class CustomerInfo
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public bool IsDeleted { get; set; }
public CustomerInfo()
{
this.IsDeleted = false;
}
}
using SQLite;
public class DbUtility
{
static SQLiteConnection _con;
public bool IsInTransaction { get { return _con.IsInTransaction; } }
public DbUtility()
{
//コネクションを取得
_con = DependencyService.Get<ISQLService>().GetConnection();
//テーブルを更新する (何度流しても良い)
_con.CreateTable<CustomerInfo>();
}
//トランザクションを開始する
public void BeginTran()
{
while (_con.IsInTransaction)
{ //他のトランザクションが終了するまで待機する
}
_con.BeginTransaction();
}
public void RollBack()
{
if (this.IsInTransaction)
{
_con.Rollback();
}
}
public void Commit()
{
_con.Commit();
}
/// <summary>
/// 顧客リストを取得する
/// </summary>
/// <returns></returns>
public ObservableCollection<CustomerInfo> GetCustomerInfoList()
{
IEnumerable<CustomerInfo> cInfo = _con.Table<CustomerInfo>().Where(m => m.IsDeleted == false);
//IEnumerable<T>型からObservableCollection<T>型に変換
return new ObservableCollection<CustomerInfo>(cInfo)
}
/// <summary>
/// 顧客情報を取得する
/// </summary>
/// <returns></returns>
public CustomerInfo GetCustomerInfo(int id)
{
return _con.Table<CustomerInfo>().Where(m => m.Id == id &&
m.IsDeleted == false).SingleOrDefault();
}
/// <summary>
/// 顧客情報を更新する
/// </summary>
/// <param name="cInfo">顧客情報</param>
/// <returns></returns>
public int UpdateCustomerInfo(CustomerInfo cInfo)
{
int cnt = 0;
if (cInfo.Id != 0)
{
//Idが0でない場合は更新
cnt = _con.Update(cInfo);
}
else
{
//Id=0の場合は新規でInsert
cnt = _con.Insert(cInfo);
}
return cnt;
}
/// <summary>
/// 顧客情報を削除する
/// </summary>
/// <param name="cInfo">顧客情報</param>
/// <returns></returns>
public int DeleteCustomerInfo(CustomerInfo cInfo)
{
int cnt = 0;
if (cInfo.Id != 0)
{
//Idが0でない場合、削除可能
cnt = _con.Delete(cInfo);
}
return cnt;
}
/// <summary>
/// 顧客情報を削除する
/// </summary>
/// <param name="id">ID</param>
/// <returns></returns>
public int DeleteCustomerInfo(int id)
{
CustomerInfo cInfo = this.GetCustomerInfo(id);
return this.DeleteCustomerInfo(cInfo);
}
}
DbUtility dbUtil = new DbUtility();
//出力
this.ListView1.ItemsSource = dbUtil.GetCustomerInfoList();
//入力
var cInfo = dbUtil.GetCustomerInfo(1);
cInfo.Name = "test";
dbUtil.BeginTran();
try
{
if (dbUtil.UpdateCustomerInfo(cInfo) > 0)
{
dbUtil.Commit();
await DisplayAlert("登録", "保存しました。", "OK");
}
else
{
dbUtil.RollBack();
await DisplayAlert("登録", "登録できませんでした。", "OK");
}
}
catch (Exception ex)
{
dbUtil.RollBack();
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。