Chloe数据库的使用

 

先上GitHub连接:https://github.com/shuxinqin/Chloe/wiki

 

1、先连接数据库,新建一个数据库,名字为TestDB

然后创建一张表

Create table [Person]
(
    [Id] int identity(1,1) primary key,
    [Name] nvarchar(64) not null,
    [Age] int not null
)

然后在vs里连接数据库。

string connString = "Data Source=DESKTOP-JJ5TQU5;Initial Catalog = TestDB;User Id=sa;Password=123456;";
                MsSqlContext dbContext = new MsSqlContext(connString);

自己写个person类

 

普通插入数据

IQuery<Person> q = dbContext.Query<Person>();
                List<int> ids = new List<int>() { 5, 6, 7 };
                //向数据库中插入数据
                Person person = new Person();
                person.Name = "nancy2323";
                person.Age = 18;
                dbContext.Insert(person);

lamdba式插入数据

                int id = (int)dbContext.Insert<Person>(() => new Person()
                {
                    Name = "王五",
                    Age = 24
                });

BulkInsert式插入数据

插入在TestEntity里

同样在DB里创建一个TestEntity的表,内有下面这些参数

                List<TestEntity> entities = new List<TestEntity>();
                for (int i = 0; i < 10; i++)
                {
                    entities.Add(new TestEntity()
                    {
                        F_Byte = 1,
                        F_Int16 = 1,
                        F_Int32 = i,
                        F_Int64 = i,
                        F_Double = i,
                        F_Float = i,
                        F_Decimal = i,
                        F_Bool = true,
                        F_DateTime = DateTime.Now,
                        F_String = "lu" + i.ToString()
                    });
                }
                dbContext.BulkInsert(entities);
                dbContext.Update(entities);

 

db内会出现

 

 

 

修改数据

 

                dbContext.TrackEntity(person);
                person.Age = person.Age + 1;
                dbContext.Update(person);

 

 

lambda式更新

                dbContext.Update<Person>(a => a.Name == "张三", a => new Person()
                {
                    Age = a.Age + 1
                });

 

改1

                Person person = dbContext.Query<Person>().Where(x => x.Name == "张三").FirstOrDefault();
                if (person != null)
                {
                    person.Age = 30;
                    dbContext.Update(person);
                }

 

改2

                dbContext.Update<Person>(x => x.Name == "张三", x => new Person
                {
                    Age = x.Age + 1
                });

 

删除

                dbContext.Delete<Person>(a => a.Name == "王五");

 

查找可自行github查阅

posted @ 2021-07-09 10:58  mischiefboy  阅读(434)  评论(0)    收藏  举报