SqlSugar使用

1 特点 Sqlsugar是一款轻量级的ORM,支持DB First、Code First、Model First,大量语法糖,Lambda表达式,支持大部分数据库。

2 sqlsugar连接

 1  public static SqlSugarClient SqlSugarClient
 2         {
 3             get
 4             {
 5                 return new SqlSugarClient(new ConnectionConfig()
 6                 {
 7                     DbType = SetDBType(ConfigurationManager.AppSettings["DBType"]), //配置数据库类型
 8                     ConnectionString = ConfigurationManager.AppSettings["ConnectionString"],  //数据库连接字符串
 9                     IsAutoCloseConnection = true,//设置为true无需使用using或者Close操作,自动关闭连接,不需要手动关闭数据链接
10                     InitKeyType = InitKeyType.SystemTable//默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
11                 });
12             }
13         }
14         public static DbType SetDBType(string dbType)
15         {
16             string sql = ConfigurationManager.AppSettings["DBType"];
17             DbType DBType;
18             switch (dbType.ToLower())
19             {
20                 case "sqlserver":
21                     DBType = DbType.SqlServer;
22                     break;
23                 case "sqlite":
24                     DBType = DbType.Sqlite;
25                     break;
26                 case "mysql":
27                     DBType = DbType.MySql;
28                     break;
29                 default:
30                     DBType = DbType.MySql;
31                     break;
32             }
33             return DBType;
34         }
View Code

 

3 CRUD操作

DbClass.SqlSugarClient.Insertable<EmployeeCheck>(info).ExecuteCommand()
View Code

DbClass.SqlSugarClient.Deleteable<CheckListInfo>().Where(d => d.ID == id).ExecuteCommand()
View Code

DbClass.SqlSugarClient.Updateable<ChangeListInfo>(info).Where(o=>o.ID== m_ID).ExecuteCommand()
View Code

 DataTable listInfos =DbClass.SqlSugarClient.Queryable<ChangeListInfo>().OrderBy(d => d.IsueDate).ToDataTable();

DataTable dt = DbClass.SqlSugarClient.SqlQueryable<InspectedProduct>(sql).ToDataTable();
View Code

 

posted @ 2022-11-15 17:56  htiu  阅读(236)  评论(0编辑  收藏  举报