查看: 1252|回复: 1
打印 上一主题 下一主题

[其他] 跨平台的IBoxdb数据库的工具类

[复制链接]
may    

8830

主题

81

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52352
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-10-18 00:02:16 |只看该作者 |倒序浏览
跨平台的IBoxdb数据库的工具类,可用于PC和移动平台等多种平台
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using iBoxDB.LocalServer;
  5. using UnityEngine;

  6. namespace MyIBoxdb
  7. {
  8.     public class DbHelper
  9.     {
  10.         private static DbHelper _instance = null;
  11.         private string rootPath;
  12.         private DB server;
  13.         private DB.AutoBox db;



  14.         private DbHelper()
  15.         {
  16.             rootPath = Application.persistentDataPath;
  17.             DB.Root(rootPath);
  18.             server = new DB(1, rootPath);
  19.             db = server.Open();
  20.         }

  21.         public static DbHelper Instance
  22.         {
  23.             get
  24.             {
  25.                 if (_instance == null)
  26.                 {
  27.                     _instance = new DbHelper();
  28.                 }
  29.                 return _instance;
  30.             }
  31.         }


  32.         /// <summary>
  33.         /// 创建表
  34.         /// </summary>
  35.         /// <typeparam name="T">表的类型</typeparam>
  36.         /// <param name="tableName">表名</param>
  37.         /// <param name="key">主键名</param>
  38.         public void EnsureTable<T>(string tableName, string key)
  39.             where T : class
  40.         {
  41.             try
  42.             {
  43.                 server.GetConfig().EnsureTable<T>(tableName, key);
  44.                 //db = server.Open();
  45.             }
  46.             catch (Exception)
  47.             {

  48.                 throw;
  49.             }
  50.         }

  51.         /// <summary>
  52.         /// 添加一条数据
  53.         /// </summary>
  54.         /// <typeparam name="T">数据类型</typeparam>
  55.         /// <param name="tableName">表名</param>
  56.         /// <param name="item">添加的一个对象</param>
  57.         public bool Insert<T>(string tableName, T item)
  58.             where T : class
  59.         {
  60.             using (var box = db.Cube())
  61.             {
  62.                 bool resualt = box.Bind(tableName).Insert<T>(item);
  63.                 box.Commit();
  64.                 return resualt;
  65.             }
  66.         }

  67.         /// <summary>
  68.         /// 添加一组数据
  69.         /// </summary>
  70.         /// <typeparam name="T">数据类型</typeparam>
  71.         /// <param name="tableName">表名</param>
  72.         /// <param name="itemList">待添加的集合</param>
  73.         public void Insert<T>(string tableName, List<T> itemList)
  74.             where T : class
  75.         {
  76.             try
  77.             {
  78.                 foreach (var item in itemList)
  79.                 {
  80.                     using (var box = db.Cube())
  81.                     {
  82.                         box.Bind(tableName).Insert(item);
  83.                         box.Commit();
  84.                     }
  85.                 }
  86.                
  87.                
  88.             }
  89.             catch (Exception)
  90.             {

  91.                 throw;
  92.             }
  93.         }

  94.         /// <summary>
  95.         /// 删除数据
  96.         /// </summary>
  97.         /// <param name="tableName">表名</param>
  98.         /// <param name="key">主键名</param>
  99.         public bool Delete(string tableName, object key)
  100.         {
  101.             try
  102.             {
  103.                 using (var box = db.Cube())
  104.                 {
  105.                     bool result = box.Bind(tableName, key).Delete();
  106.                     box.Commit();
  107.                     return result;
  108.                 }
  109.             }
  110.             catch (Exception)
  111.             {
  112.                 throw;
  113.             }
  114.         }


  115.         /// <summary>
  116.         /// 修改数据
  117.         /// </summary>
  118.         /// <typeparam name="T">数据类型</typeparam>
  119.         /// <param name="newItem">新数据</param>
  120.         /// <param name="tableName">表名</param>
  121.         public bool UpdateData<T>(string tableName,T newItem,object key )
  122.             where T : class
  123.         {
  124.             try
  125.             {
  126.                 using (var box = db.Cube())
  127.                 {
  128.                     bool result = box.Bind(tableName,key).Update(newItem);
  129.                     box.Commit();
  130.                     return result;
  131.                 }
  132.             }
  133.             catch (Exception)
  134.             {
  135.                 throw;
  136.             }
  137.         }


  138.         /// <summary>
  139.         /// 查询整张表的数据,返回一个List
  140.         /// </summary>
  141.         /// <typeparam name="T">数据类型</typeparam>
  142.         /// <param name="tableName">表名</param>
  143.         /// <returns></returns>
  144.         public List<T> Select<T>(string tableName)
  145.             where T : class,new()
  146.         {
  147.             try
  148.             {
  149.                 using (var box = db.Cube())
  150.                 {
  151.                     return box.Select<T>("from " + tableName).ToList();
  152.                 }
  153.             }
  154.             catch (Exception)
  155.             {

  156.                 throw;
  157.             }
  158.         }

  159.         /// <summary>
  160.         /// 条件查询(需要自己写SQL语句)
  161.         /// </summary>
  162.         /// <typeparam name="T">数据类型</typeparam>
  163.         /// <param name="tableName">表名</param>
  164.         /// <param name="condition">条件委托</param>
  165.         /// <returns></returns>
  166.         public List<T> Select<T>(string tableName, Func<T, bool> condition)
  167.             where T : class,new()
  168.         {
  169.             try
  170.             {
  171.                 using (var box = db.Cube())
  172.                 {
  173.                     List<T> list = box.Select<T>("from " + tableName).Where(condition).ToList();
  174.                     box.Commit();
  175.                     return list;
  176.                 }

  177.             }
  178.             catch (Exception ex)
  179.             {
  180.                 //处理异常
  181.                 throw ex;
  182.             }
  183.         }


  184.         /// <summary>
  185.         /// 查询记录条数
  186.         /// </summary>
  187.         /// <param name="tableName">表名</param>
  188.         /// <param name="arguments">参数列表</param>
  189.         /// <returns></returns>
  190.         public int GetCount(string tableName, params object[] arguments)
  191.         {
  192.             DB.Root(rootPath);
  193.             try
  194.             {
  195.                 using (var box = db.Cube())
  196.                 {
  197.                     int count = (int)box.SelectCount("from " + tableName, arguments);
  198.                     box.Commit();
  199.                     return count;
  200.                 }
  201.             }
  202.             catch (Exception)
  203.             {
  204.                 throw;
  205.             }
  206.         }
  207.     }
  208. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

7

主题

2

听众

6159

积分

高级设计师

Rank: 6Rank: 6

纳金币
498
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2015-10-20 17:41:32 |只看该作者
这个数据库 没有可视化管理工具 感觉很low
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-8-9 15:35 , Processed in 0.292743 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部