PetaPoco与SQLite

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;

        }

    }

}

 

你可能感兴趣的:(sqlite)