SqlSugar.NET之InitTables,Insert,Update,PageEach

InitTables

Code First

// 属性介绍
// 我们会根据字段的属性生成数据库字段
// IsNullable表示表字段是否可空
// IsIgnore 为true表示不会生成字段到数据库
// IsIdentity表示为自增列
// IsPrimaryKey表示为主键
// Length 表示字段长度
// DecimalDigits 表示字段的精度 4.4
// ColumnDataType  强制设置数据库字段的类型(考虑到切换数据库有些类型其它库没有最新版本支持多个以逗号隔离,比如=“number,int”)
  dbColumnInfo = new DbColumnInfo() { DbColumnName = "img1", DataType = "text", Length = 1500 }
public class CodeTable
{
        [SugarColumn(IsNullable =false ,IsPrimaryKey =true,IsIdentity =true)]
        public int Id { get; set; }
        [SugarColumn(Length = 21)]
        public string Name{ get; set; }
        [SugarColumn(IsNullable = true)]
        public bool IsOk { get; set; }
        public Guid Guid { get; set; }
        public decimal Decimal { get; set; }
        [SugarColumn(IsNullable = true)]
        public DateTime? DateTime { get; set; }
        [SugarColumn(IsNullable = true)]
        public double? Dob2 { get; set; }
        [SugarColumn(Length =10)]
        public string A { get; set; }
}
public class CodeTable2 {
        public int Id { get; set; }
        public string Name { get; set; }
        [SugarColumn(IsIgnore =true)]
        public string TestId { get; set; }
}
[SugarTable("T_logs")]
public class logitem
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = false, ColumnName = "guid")]
    public string guid { get; set; }
    public string type { get; set; }
    public string log { get; set; }
    public string addtime { get; set; }
}

SugarColumn

[SugarColumn(IsIgnore = true)]
[SugarColumn(IsPrimaryKey = true, IsIdentity = false, ColumnName = "guid")]
[SugarColumn(IsPrimaryKey = true, IsIdentity = false, ColumnName = "guid", Length = 32)]
[SugarColumn(Length = 20)]
[SugarColumn(ColumnDataType = "text")]
[SugarColumn(ColumnDataType = "text", IsNullable = true)]
[SugarColumn(ColumnDataType = "text", IsNullable = true, Length = 255)]
[SugarColumn(IsNullable = true, Length = 255)]
[SugarColumn(IsNullable = false, DefaultValue = "0", Length = 2)]
[SugarColumn(ColumnDataType = "text", DefaultValue = "0", IsNullable = true)]
[SugarColumn(ColumnDataType = "text", DefaultValue = "false", IsNullable = true)]

SugarColumn text

  • SQLite Text 类型不支持 Length
[SugarColumn(ColumnDataType = "text", IsNullable = true)]
public string supporttag { get; set; }

SugarColumn 时间

[SugarColumn(IsNullable = false)]
public DateTime? createtime { get; set; }
[SugarColumn(IsNullable = false)]
public DateTime createtime { get; set; }

SugarColumn INT

  • 不能设置默认值否则报错
[SugarColumn(IsNullable = false, Length = 2)]
public int deployed { get; set; }

SugarColumn bool / Boolean

  • SQLite 没有单独的 Boolean 存储类。相反,布尔值被存储为整数 0(false)和 1(true)
[SugarColumn(IsNullable = true, Length = 1)]
public bool active { get; set; }
// 文本
[SugarColumn(IsNullable = true, Length = 5)]
public bool active { get; set; }

SugarColumn 忽略

[SugarColumn(IsIgnore=true)]
public bool active { get; set; }

Insert

代码插入

demo 一

var t2 = db.Insertable(insertObj).ExecuteCommand();
db.Insertable<DemoTest.Database.Main.Entity.Models.demo_guiditem>(item).AddQueue();
db.Insertable(demo_Guiditems);
db.Insertable<DemoTest.Database.Main.Entity.Models.urlsguiditem>(item).AddQueue();

demo 二

db.Utilities.PageEach(demo_Guiditems, 500, list =>
{
    var t2 = db.Insertable(list).ExecuteCommand();
});

更新或者插入

var Students = db.Saveable<Student>(entity).ExecuteReturnEntity();
var count = db.Saveable<Student>(entity).ExecuteCommand();
var t0 = db.Saveable<Student>(entity).ExecuteCommand();

Update

更新

只更新某列

  • 只更新实体里面的 Name 列(主键要有值,主键是更新条件)
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();

更新代码 1

db.Utilities.PageEach(urlsguiditems_PageEach, 200, list =>
{
    var t2 = db.Updateable(list).UpdateColumns(it => new { it.status }).ExecuteCommand();
});

PageEach

// Redis
var startIndex = (root.Pagination.PageIndex - 1) * root.Pagination.PageSize;
var endIndex = startIndex + root.Pagination.PageSize;
// SQL
var startIndex = (root.Pagination.PageIndex - 1) * root.Pagination.PageSize;
var endIndex = root.Pagination.PageSize;
PageExtenions.PageEach(1000, 20, CurrentExecutCount =>
{
    try
    {
    }
    catch (Exception ex)
    {
    }
});
CsharpLazycode.Module.Page.Utilities.PageEach
CsharpLazycode.Module.Page.Utilities.PageEach(urlsguiditems, 2, list =>
  {
      try
      {
      }
      catch (Exception ex)
      {
      }
  });
PageExtenions.PageEach(GoCount, 1000, CurrentExecutCount =>
{
  ListStrings = new List<string>();
  for (int i = 0; i < CurrentExecutCount; i++)
  { }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值