PetaPoco與SQLite

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.