我们可以使用SqlSugarClient对数据库进行增、删、查、改等功能
//创建数据库对象 SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "Server=.xxxxx",//连接符字串 DbType = DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息 });
安装完直接复制下面代码就能在程序中运行
//查询所有 public List<Student> GetStudentList() { var db= GetInstance();//获取SqlSugarClient var list= db.Queryable<Student>().ToList();//查询表的所有 return list; } //创建SqlSugarClient private SqlSugarClient GetInstance() { //创建数据库对象 SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "Server=.xxxxx",//连接符字串 DbType = DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息 }); //添加Sql打印事件,开发中可以删掉这个代码 db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql); }; return db; } //实体与数据库结构一样 public class Student { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//主键并且自增 (string不能设置自增) public int Id { get; set; } public int? SchoolId { get; set; } public string Name { get; set; } }
SqlSugarClient是通过ConnectionConfig进行传参数详细参数如下
名称 | 描述 | 必填 |
---|---|---|
DbType | 数据库类型 | 是 |
ConnectionString | 连接字符串 | 是 |
IsAutoCloseConnection | 自动释放和关闭数据库连接,如果有事务事务结束时关闭,否则每次操作后关闭 | |
InitKeyType | ORM读取自增列和主键的方式 ,建议从特性读取,如果从数据库读取需要SA等高级权限账号 | |
IsShardSameThread | 同线程共享SqlConnection但是不共享SqlSugarClient,非特殊情况不建议使用,特别是异步 | |
ConfigureExternalServices | 一些扩展层务的集成 | |
MoreSettings | 更多设置 | |
SlaveConnectionConfigs | 主从设置 |
db.Ado.CommandTimeOut = 30;//单位秒
2016 © donet5.comApache Licence 2.0