C#:SqlSugar中时间戳(TimeStamp)的使用

时间:2021-12-22 19:41:13

1.数据库建表

CREATE TABLE dbo.Test
(
      tId        INT IDENTITY NOT NULL
    , tName      NVARCHAR (20) NOT NULL
    , tSalary    DECIMAL (8, 2) NULL
    , tTimeStamp TIMESTAMP
)

2.创建类

    public partial class Test
    {
        [SugarColumn(IsOnlyIgnoreInsert =true)]
        public int tId { get; set; }
        public string tName { get; set; }
        public decimal? tSalary { get; set; }
        [SugarColumn(IsOnlyIgnoreInsert = true)]
        public byte[] tTimeStamp { get; set; }
    }

3.添加数据

SqlSugar.DB.Insertable<Test>(new Test { tName = "Jerry", tSalary = (decimal)7238.04 }).ExecuteCommand();

在Test类的 tTimeStamp 属性上添加[SugarColumn(IsOnlyIgnoreInsert = true)],否则会出错。

4.数据输出

var list = await SqlSugar.DB.Queryable<Test>().ToListAsync();
list.ForEach(x => Console.WriteLine($"{x.tId}   {x.tName}   {x.tSalary} {BitConverter.ToString(x.tTimeStamp).Replace("-","")}"));

C#:SqlSugar中时间戳(TimeStamp)的使用

5.条件查询

根据时间戳查询,目标是第二条

var list = await SqlSugar.DB.Queryable<Test>().ToListAsync();
var query = await SqlSugar.DB.Queryable<Test>().Where(x => x.tTimeStamp == list[1].tTimeStamp).ToListAsync();
query.ForEach(x => Console.WriteLine($"{x.tId}   {x.tName}   {x.tSalary} {BitConverter.ToString(x.tTimeStamp).Replace("-","")}"));

C#:SqlSugar中时间戳(TimeStamp)的使用