.net sqlsugar轻量级框架详解

  1. 项目右键——找到管理NuGet程序包——搜索sqlsugar安装包——安装后创建ConnectSqlCore类:
  2. //远程连接:
  3. pub­lic sta­t­ic string Con­nec­tion­String = “server=远程IP;PORT=端口;user id=用户名;password=密码;database=数据库名称; pooling=true;”; //Mysql连接
  4. pub­lic sta­t­ic string Con­nec­tion­String = “Data Source=ip;Database=数据库名称;User ID=用户名;Password=密码”;//sql连接
   /// <summary>
    /// 连接MySql
    /// </summary>
    /// <returns></returns>
    //public static string ConnectionString = "server=localhost;user id=root;password=123456;database=test; pooling=true;";

    //public static string ConnectionString = "server=192.168.1.142;PORT=3306;user id=root;password=123456;database=MySqlOne; pooling=true;";


    public SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
    {
        ConnectionString = ConnectionString,
        DbType = DbType.SqlServer,//设置数据库类型
        IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
        InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
    });

具体方法中运用: public ConnectSqlCore connectSql = new ConnectSqlCore(); //实例化

 Model示例:
 [SugarTable("UserManage")]   //  UserManage 数据库表
     public  class Usermanage
     {
         [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//如果是主键,此处必须指定,否则会引发InSingle(id)方法异常。 查询方法不需要设置主键,删除、修改必须确定
         public int Id { get; set; }
         public string UserName { get; set; }
         public string UserTel { get; set; }
     }
//增:
public bool AddMysql(Usermanage usermanage)
{
return connectSql.db.Insertable(usermanage).ExecuteCommandIdentityIntoEntity();
}
//更新、删除:
public int RenewalTask(OperationLogModel operationLogModel)
{
return connectSql.db.Updateable(operationLogModel).UpdateColumns(it => new { it.IsDeleted }).ExecuteCommand();//去掉UpdateColumns就变成了更新该条数据的全部
}
//单表查询:
public List GetProgressesList()
  {
     return connectSql.db.Queryable().ToList();
  }
 //多表联查: 
    public List GetCarThreeList()
     {
        return connectSql.db.Queryable((carInsure,car, carclass) => new JoinQueryInfos(JoinType.Left, carInsure.CarId==car.Id, JoinType.Left, car.CarClassId == carclass.Id)).Select().ToList();
     }

为您推荐

发表评论

您的电子邮箱地址不会被公开。