dapper 操作类封装

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Data.SQLite;
  5 using System.Linq;
  6 using System.Text;
  7 using Dapper;
  8 using PaiXie.Utils;
  9 namespace PaiXie.Pos.Client.Core
 10 {
 11     public class Data<T> where T : class
 12     {
 13         #region 构造函数
 14 
 15         private static Data<T> _instance;
 16         public static Data<T> GetInstance()
 17         {
 18             if (_instance == null)
 19             {
 20                 _instance = new Data<T>();
 21             }
 22             return _instance;
 23         }
 24 
 25         #endregion
 26 
 27         /// <summary>
 28         /// 分页列表
 29         /// </summary>
 30         /// <param name="data"></param>
 31         /// <param name="model"></param>
 32         /// <param name="count"></param>
 33         /// <param name="context"></param>
 34         /// <param name="transaction"></param>
 35         /// <returns></returns>
 36         public List<T> GetQueryManyForPage(SelectBuilder data, T model, out int count, SQLiteConnection context = null, IDbTransaction transaction = null)
 37         {
 38             if (context == null) context = Db.GetInstance().Context();
 39             string sqlStr = Db.GetInstance().GetSqlForSelectBuilder(data);
 40             string sqlStrCount = Db.GetInstance().GetSqlForTotalBuilder(data);
 41             List<T> list = context.Query<T>(sqlStr, model, transaction).ToList();
 42             count = ZConvert.StrToInt(context.ExecuteScalar(sqlStrCount, model, transaction), 0);
 43             return list;
 44         }
 45         /// <summary>
 46         /// 添加
 47         /// </summary>
 48         /// <param name="query"></param>
 49         /// <param name="model"></param>
 50         /// <param name="context"></param>
 51         /// <param name="transaction"></param>
 52         /// <returns></returns>
 53         public int Add(string query, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
 54         {
 55             if (context == null) context = Db.GetInstance().Context();
 56             //  query += " ; select last_insert_rowid() newid";
 57             //  string id = context.ExecuteScalar(query, model, transaction).ToString();
 58             //  return ZConvert.StrToInt(id, 0);
 59             int id = context.Execute(query, model, transaction);
 60             return id;
 61         }
 62 
 63 
 64  
 65 
 66 
 67          
 68         /// <summary>
 69         /// 修改
 70         /// </summary>
 71         /// <param name="query"></param>
 72         /// <param name="model"></param>
 73         /// <param name="context"></param>
 74         /// <param name="transaction"></param>
 75         /// <returns></returns>
 76         public int Update(string query, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
 77         {
 78             if (context == null) context = Db.GetInstance().Context();
 79             int id = context.Execute(query, model, transaction);
 80             return id;
 81         }
 82         /// <summary>
 83         /// 删除
 84         /// </summary>
 85         /// <param name="tablename"></param>
 86         /// <param name="wheresql"></param>
 87         /// <param name="model"></param>
 88         /// <param name="context"></param>
 89         /// <param name="transaction"></param>
 90         /// <returns></returns>
 91         public int Delete(string tablename, string wheresql, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
 92         {
 93             if (context == null) context = Db.GetInstance().Context();
 94             string query = "DELETE FROM  " + tablename + "  WHERE " + wheresql;
 95             int id = context.Execute(query, model, transaction);
 96             return id;
 97         }
 98         /// <summary>
 99         /// 获取单个实体
100         /// </summary>
101         /// <param name="tablename"></param>
102         /// <param name="wheresql"></param>
103         /// <param name="model"></param>
104         /// <param name="context"></param>
105         /// <param name="transaction"></param>
106         /// <returns></returns>
107         public T GetModel(string tablename, string wheresql, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
108         {
109             if (context == null) context = Db.GetInstance().Context();
110             string query = "SELECT  * FROM " + tablename + " WHERE " + wheresql + "  LIMIT 0,1";
111             T obj = context.Query<T>(query, model, transaction).SingleOrDefault();
112             return obj;
113         }
114 
115         /// <summary>
116         /// 实体列表
117         /// </summary>
118         /// <param name="query"></param>
119         /// <param name="model"></param>
120         /// <param name="context"></param>
121         /// <param name="transaction"></param>
122         /// <returns></returns>
123         public List<T> GetModelList(string query, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
124         {
125             if (context == null) context = Db.GetInstance().Context();
126             List<T> list = context.Query<T>(query, model, transaction).ToList();
127             return list;
128         }
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142     }
143 }
View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data.SQLite;
  4 using System.Linq;
  5 using System.Text;
  6 using  Dapper;
  7 using System.Windows.Forms;
  8 namespace PaiXie.Pos.Client.Core {
  9     public class Db {
 10 
 11         #region 构造函数
 12 
 13         private static Db _instance;
 14         public static Db GetInstance() {
 15             if (_instance == null) {
 16                 _instance = new Db();
 17             }
 18             return _instance;
 19         }
 20 
 21         #endregion
 22 
 23         #region Context
 24         public SQLiteConnection Context() {
 25             SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
 26             sb.DataSource =
 27                 @"//10.0.0.105/db/DB.s3db";
 28             // Application.StartupPath + @"\Data.dll";//DB.db3
 29             SQLiteConnection con = new SQLiteConnection(sb.ToString());        
 30             return con;
 31         }
 32         #endregion
 33 
 34         #region 分页语句拼接
 35         public string GetSqlForSelectBuilder(SelectBuilder data) {
 36             var sql = "";
 37             sql = "select " + data.Select;
 38             sql += " from " + data.From;
 39             if (data.WhereSql.Length > 0)
 40                 sql += " where " + data.WhereSql;
 41             if (data.GroupBy.Length > 0)
 42                 sql += " group by " + data.GroupBy;
 43             if (data.Having.Length > 0)
 44                 sql += " having " + data.Having;
 45             if (data.OrderBy.Length > 0)
 46                 sql += " order by " + data.OrderBy;
 47             if (data.PagingItemsPerPage > 0
 48                 && data.PagingCurrentPage > 0
 49                 ) {
 50                     sql += string.Format(" limit {0} offset {1}", data.PagingItemsPerPage, ((data.PagingCurrentPage * data.PagingItemsPerPage) - data.PagingItemsPerPage + 1) - 1);
 51             }
 52             return sql;
 53         }
 54         public string GetSqlForTotalBuilder(SelectBuilder data) {
 55             var sql = "";
 56             sql = "select count(*)";
 57             sql += " from " + data.From;
 58             if (data.WhereSql.Length > 0)
 59                 sql += " where " + data.WhereSql;
 60             return sql;
 61         }
 62         #endregion
 63 
 64     }
 65 
 66     #region SelectBuilder
 67     public class SelectBuilder {
 68         public int PagingCurrentPage { get; set; }
 69         public int PagingItemsPerPage { get; set; }
 70         
 71 
 72         private string _Having = "";
 73         public string Having {
 74             set { _Having = value; }
 75             get { return _Having; }
 76         }
 77 
 78 
 79     
 80         private string _GroupBy = "";
 81         public string GroupBy {
 82             set { _GroupBy = value; }
 83             get { return _GroupBy; }
 84         }
 85 
 86 
 87         
 88         private string _OrderBy = "";
 89         public string OrderBy {
 90             set { _OrderBy = value; }
 91             get { return _OrderBy; }
 92         }
 93 
 94 
 95 
 96         
 97         private string _From = "";
 98         public string From {
 99             set { _From = value; }
100             get { return _From; }
101         }
102 
103 
104     
105         private string _Select="";
106         public string Select {
107             set { _Select = value; }
108             get { return _Select; }
109         }
110 
111         private string _WhereSql="";
112         public string WhereSql {
113             set { _WhereSql = value; }
114             get { return _WhereSql; }
115         }
116 
117 
118     
119     }
120     #endregion
121 }
View Code

 

你可能感兴趣的:(dapper 操作类封装)