- 最后登录
- 2019-12-25
- 注册时间
- 2012-8-24
- 阅读权限
- 90
- 积分
- 71088
 
- 纳金币
- 52352
- 精华
- 343
|
跨平台的IBoxdb数据库的工具类,可用于PC和移动平台等多种平台 - using System;
- using System.Collections.Generic;
- using System.Linq;
- using iBoxDB.LocalServer;
- using UnityEngine;
- namespace MyIBoxdb
- {
- public class DbHelper
- {
- private static DbHelper _instance = null;
- private string rootPath;
- private DB server;
- private DB.AutoBox db;
- private DbHelper()
- {
- rootPath = Application.persistentDataPath;
- DB.Root(rootPath);
- server = new DB(1, rootPath);
- db = server.Open();
- }
- public static DbHelper Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new DbHelper();
- }
- return _instance;
- }
- }
- /// <summary>
- /// 创建表
- /// </summary>
- /// <typeparam name="T">表的类型</typeparam>
- /// <param name="tableName">表名</param>
- /// <param name="key">主键名</param>
- public void EnsureTable<T>(string tableName, string key)
- where T : class
- {
- try
- {
- server.GetConfig().EnsureTable<T>(tableName, key);
- //db = server.Open();
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// 添加一条数据
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="tableName">表名</param>
- /// <param name="item">添加的一个对象</param>
- public bool Insert<T>(string tableName, T item)
- where T : class
- {
- using (var box = db.Cube())
- {
- bool resualt = box.Bind(tableName).Insert<T>(item);
- box.Commit();
- return resualt;
- }
- }
- /// <summary>
- /// 添加一组数据
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="tableName">表名</param>
- /// <param name="itemList">待添加的集合</param>
- public void Insert<T>(string tableName, List<T> itemList)
- where T : class
- {
- try
- {
- foreach (var item in itemList)
- {
- using (var box = db.Cube())
- {
- box.Bind(tableName).Insert(item);
- box.Commit();
- }
- }
-
-
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// 删除数据
- /// </summary>
- /// <param name="tableName">表名</param>
- /// <param name="key">主键名</param>
- public bool Delete(string tableName, object key)
- {
- try
- {
- using (var box = db.Cube())
- {
- bool result = box.Bind(tableName, key).Delete();
- box.Commit();
- return result;
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// 修改数据
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="newItem">新数据</param>
- /// <param name="tableName">表名</param>
- public bool UpdateData<T>(string tableName,T newItem,object key )
- where T : class
- {
- try
- {
- using (var box = db.Cube())
- {
- bool result = box.Bind(tableName,key).Update(newItem);
- box.Commit();
- return result;
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// 查询整张表的数据,返回一个List
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="tableName">表名</param>
- /// <returns></returns>
- public List<T> Select<T>(string tableName)
- where T : class,new()
- {
- try
- {
- using (var box = db.Cube())
- {
- return box.Select<T>("from " + tableName).ToList();
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// 条件查询(需要自己写SQL语句)
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="tableName">表名</param>
- /// <param name="condition">条件委托</param>
- /// <returns></returns>
- public List<T> Select<T>(string tableName, Func<T, bool> condition)
- where T : class,new()
- {
- try
- {
- using (var box = db.Cube())
- {
- List<T> list = box.Select<T>("from " + tableName).Where(condition).ToList();
- box.Commit();
- return list;
- }
- }
- catch (Exception ex)
- {
- //处理异常
- throw ex;
- }
- }
- /// <summary>
- /// 查询记录条数
- /// </summary>
- /// <param name="tableName">表名</param>
- /// <param name="arguments">参数列表</param>
- /// <returns></returns>
- public int GetCount(string tableName, params object[] arguments)
- {
- DB.Root(rootPath);
- try
- {
- using (var box = db.Cube())
- {
- int count = (int)box.SelectCount("from " + tableName, arguments);
- box.Commit();
- return count;
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- }
复制代码 |
|