首页 > 代码库 > PetaPoco与SQLite
PetaPoco与SQLite
PetaPoco与SQLite.
对于精简版本的ORM,PetaPoco确实短小精悍,想做个WPF的Demo,然后将PetaPoco与SQLite集成一起使用,简单易用,是不错的选择。
(1)==数据库配置文件 <!--数据库配置--> <appSettings> <!--SQLite--> <add key="SQLString" value=http://www.mamicode.com/"~\App_Data\OldDemo.db"/> <add key="SQLString2" value=http://www.mamicode.com/"|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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。