1 特点 Sqlsugar是一款轻量级的ORM,支持DB First、Code First、Model First,大量语法糖,Lambda表达式,支持大部分数据库。
2 sqlsugar连接
public static SqlSugarClient SqlSugarClient
{
get
{
return new SqlSugarClient(new ConnectionConfig()
{
DbType = SetDBType(ConfigurationManager.AppSettings["DBType"]), //配置数据库类型
ConnectionString = ConfigurationManager.AppSettings["ConnectionString"], //数据库连接字符串
IsAutoCloseConnection = true,//设置为true无需使用using或者Close操作,自动关闭连接,不需要手动关闭数据链接
InitKeyType = InitKeyType.SystemTable//默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
});
}
}
public static DbType SetDBType(string dbType)
{
string sql = ConfigurationManager.AppSettings["DBType"];
DbType DBType;
switch (dbType.ToLower())
{
case "sqlserver":
DBType = DbType.SqlServer;
break;
case "sqlite":
DBType = DbType.Sqlite;
break;
case "mysql":
DBType = DbType.MySql;
break;
default:
DBType = DbType.MySql;
break;
}
return DBType;
}
3 CRUD操作
增
DbClass.SqlSugarClient.Insertable<EmployeeCheck>(info).ExecuteCommand()
删
DbClass.SqlSugarClient.Deleteable<CheckListInfo>().Where(d => d.ID == id).ExecuteCommand()
改
DbClass.SqlSugarClient.Updateable<ChangeListInfo>(info).Where(o=>o.ID== m_ID).ExecuteCommand()
查
DataTable listInfos =DbClass.SqlSugarClient.Queryable<ChangeListInfo>().OrderBy(d => d.IsueDate).ToDataTable();
DataTable dt = DbClass.SqlSugarClient.SqlQueryable<InspectedProduct>(sql).ToDataTable();