一般的,就使用GetTableInfoList来获取所有表。然后对比一下就知道表是否存在了。
使用sqlsugar时,都需要创建实体类。并且添加上特性 [SugarTable]
我这边想从一开始,检测数据库中是否有这个表。但是我并不想使用(GetTableInfoList)直接列出所有的表,进行对比。
因为使用泛型会方法很多
DBContext<RoleModel>.GetInstance().CurrentDb.Insert(guest);
这时,就需要根据泛型来获取类的特性
Type type = typeof(T); string tableName = ((SugarTable)(type.GetCustomAttributes(true)[0])).TableName;
type.GetCustomAttributes(true)。这里是获取所有特性。
因为类使用的特性只有SugarTable这一个,所以直接拿第一个数据就可以,再转换成SugarTable。后面获取TableName。
public bool IsExist() { Type type = typeof(T); string tableName = ((SugarTable)(type.GetCustomAttributes(true)[0])).TableName; return Db.DbMaintenance.IsAnyTable(tableName); }
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Runtime.InteropServices; using System.Text; namespace 快递条码识别.sql { public class DBContext<T> where T : class, new() { public SqlSugarClient Db; private static DBContext<T> mSingle = null; public static DBContext<T> GetInstance() { if (mSingle == null) mSingle = new DBContext<T>(); return mSingle; } protected DBContext() { //通过这个可以直接连接数据库 Db = new SqlSugarClient(new ConnectionConfig() { //可以在连接字符串中设置连接池pooling=true;表示开启连接池 //eg:min pool size=2;max poll size=4;表示最小连接池为2,最大连接池是4;默认是100 //ConnectionString = "database='" + "BookShop" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "1234" + "';charset='utf8';pooling=true", ConnectionString = "database='" + "package_crm" + "';Data Source = '" + "127.0.0.1" + "'; User Id = '" + "root" + "'; pwd='" + "root" + "';charset='utf8';pooling=true", DbType = SqlSugar.DbType.MySql,//我这里使用的是Mysql数据库 IsAutoCloseConnection = true,//自动关闭连接 InitKeyType = InitKeyType.Attribute }); //调式代码 用来打印SQL //Db.Aop.OnLogExecuting = (sql, pars) => //{ // Console.WriteLine(sql + "\r\n" + // Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); // Console.WriteLine(); //}; } public void Dispose() { if (Db != null) { Db.Dispose(); } } public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } } /// <summary> /// 查看表是否存在 /// </summary> /// <returns></returns> public bool IsExist() { Type type = typeof(T); string tableName = ((SugarTable)(type.GetCustomAttributes(true)[0])).TableName; //怎么获取sqlsugar实体类中的表名 //((SugarTable<T>)).TableName; return Db.DbMaintenance.IsAnyTable(tableName); } /// <summary> /// 获取所有 /// </summary> /// <returns></returns> public virtual List<T> GetList() { return CurrentDb.GetList(); } /// <summary> /// 根据表达式查询 /// </summary> /// <returns></returns> public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression) { return CurrentDb.GetList(whereExpression); } /// <summary> /// 根据表达式查询分页 /// </summary> /// <returns></returns> public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel) { return CurrentDb.GetPageList(whereExpression, pageModel); } /// <summary> /// 根据表达式查询分页并排序 /// </summary> /// <param name="whereExpression">it</param> /// <param name="pageModel"></param> /// <param name="orderByExpression">it=>it.id或者it=>new{it.id,it.name}</param> /// <param name="orderByType">OrderByType.Desc</param> /// <returns></returns> public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) { return CurrentDb.GetPageList(whereExpression, pageModel, orderByExpression, orderByType); } /// <summary> /// 根据主键查询 /// </summary> /// <returns></returns> public virtual List<T> GetById(dynamic id) { return CurrentDb.GetById(id); } /// <summary> /// 根据主键删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(dynamic id) { if (string.IsNullOrEmpty(id.ObjToString)) { Console.WriteLine(string.Format("要删除的主键id不能为空值!")); } return CurrentDb.Delete(id); } /// <summary> /// 根据实体删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(T data) { if (data == null) { Console.WriteLine(string.Format("要删除的实体对象不能为空值!")); } return CurrentDb.Delete(data); } /// <summary> /// 根据主键删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(dynamic[] ids) { if (ids.Count() <= 0) { Console.WriteLine(string.Format("要删除的主键ids不能为空值!")); } return CurrentDb.AsDeleteable().In(ids).ExecuteCommand() > 0; } /// <summary> /// 根据表达式删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Delete(Expression<Func<T, bool>> whereExpression) { return CurrentDb.Delete(whereExpression); } /// <summary> /// 根据实体更新,实体需要有主键 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Update(T obj) { if (obj == null) { Console.WriteLine(string.Format("要更新的实体不能为空,必须带上主键!")); } return CurrentDb.Update(obj); } /// <summary> ///批量更新 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Update(List<T> objs) { if (objs.Count <= 0) { Console.WriteLine(string.Format("要批量更新的实体不能为空,必须带上主键!")); } return CurrentDb.UpdateRange(objs); } /// <summary> /// 插入 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Insert(T obj) { return CurrentDb.Insert(obj); } /// <summary> /// 批量 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual bool Insert(List<T> objs) { return CurrentDb.InsertRange(objs); } //可以扩展更多方法 } }
标签:return,CurrentDb,是否,Db,virtual,查找,bool,public,sqlsugar From: https://www.cnblogs.com/kuangzhuaikuxuandiaozhatian/p/17065979.html