標籤:style blog io ar color os 使用 sp for
PetaPoco與SQLite.
對於精簡版本的ORM,PetaPoco確實短小精悍,想做個WPF的Demo,然後將PetaPoco與SQLite整合一起使用,簡單易用,是不錯的選擇。
(1)==資料庫設定檔 <!--資料庫配置--> <appSettings> <!--SQLite--> <add key="SQLString" value="~\App_Data\OldDemo.db"/> <add key="SQLString2" value="|DataDirectory|OldDemo.db"/> </appSettings>(2)配置類class Config { public static string DatabaseFile = ""; public static string DataSource { get { return string.Format("data source={0}", ConfigurationManager.AppSettings["SQLString2"]); } } }(3)==BaseServices類using System;using System.Collections.Generic;using System.Data;using System.Data.SQLite;using System.Linq;using System.Runtime.Remoting.Messaging;using System.Text;using App.Core.Constant;using PetaPoco;namespace App.Core.Service{ public abstract class BaseService { private IDbConnection connection; public BaseService() { connection = new SQLiteConnection(Config.DataSource); } /// <summary> /// 查詢所有對象 /// </summary> /// <typeparam name="T">泛型對象</typeparam> /// <returns></returns> public List<T> Query<T>() { var list = new List<T>(); try { string className = typeof(T).ToString(); var db = new Database(connection); string name = className.Substring( className.LastIndexOf(‘.‘) + 1, className.Length - className.LastIndexOf(‘.‘) - 1); connection.Open(); list = db.Fetch<T>(string.Format("select * from {0} ", name)); } catch (Exception ex) { throw ex; } return list; } /// <summary> /// 刪除對象 /// </summary> /// <typeparam name="T">泛型對象</typeparam> /// <param name="objData"></param> /// <returns></returns> public int Delete<T>(object objData) { var result = 0; try { var db = new Database(connection); connection.Open(); result = db.Delete<T>(objData); } catch (Exception ex) { throw ex; } return result; } /// <summary> /// 添加一條記錄 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objData"></param> /// <returns></returns> public T Add<T>(object objData) { T result; try { var db = new Database(connection); connection.Open(); result = (T)Convert.ChangeType(db.Insert(objData), typeof(T)); } catch (Exception ex) { throw ex; } return result; } }}
PetaPoco與SQLite