欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

轻量ORM-SqlRepoEx (四)INSERT、UPDATE、DELETE 语句

程序员文章站 2022-05-13 22:53:39
.Net平台下兼容.NET Standard 2.0,一个实现以Lambda表达式转转换标准SQL语句,使用强类型操作数据的轻量级ORM工具,在减少魔法字串同时,通过灵活的Lambda表达式组合,实现业务数据查询的多样性。 ......

*本文中所用类声明见上一篇博文《轻量orm-sqlrepoex (三)select语句》中customers类

一、增加记录

1、工厂一个实例仓储

  var repository = repofactory.create<customers>();

2、使用实例增加

customers customers = new customers { customerid = "your1", companyname = "你的公司名1", phone = "13900000000" };

repository.insert().for(customers).go();

当前增加的sql语句:

insert[dbo].[customers]([customerid], [companyname], [contactname], [contacttitle], [address], [city], [region], [postalcode], [country], [phone], [fax])

values('your1', '你的公司名1', null, null, null, null, null, null, null, '13900000000', null);

3、指定列值增加

    repository.insert().with(m => m.customerid, "your1")

                .with(m => m.companyname, "你的公司名1")

                .with(m => m.phone, "13900000001").go();

当前增加的sql语句:

        insert[dbo].[customers]

        ([customerid], [companyname], [phone])

       values('your1', '你的公司名1', '13900000001');

4、注意事项:

(1)、如果有自增加字段,需要在对应属性增加[identityfiled] 特性

如: [identityfiled]

     public string customerid { get; set; }

(2)则生成时有 identityfiled 特性是的sql语句

insert [dbo].[customers]([companyname], [contactname], [contacttitle], [address], [city], [region], [postalcode], [country], [phone], [fax])

values('你的公司名', null, null, null, null, null, null, null, '1390000000', null);

select [customerid], [companyname], [contactname], [contacttitle], [address], [city], [region], [postalcode], [country], [phone], [fax]

from [dbo].[customers]

where [customerid] = scope_identity();

(3)指定列值增加时有 identityfiled 特性是的sql语句

insert [dbo].[customers]([customerid], [companyname], [phone])

values('your1', '你的公司名1', '13900000001');

select [customerid], [companyname], [phone]

from [dbo].[customers]

where [customerid] = scope_identity();

关于相关特性会在后续博文中介绍

二、更新记录

1、工厂一个实例仓储 

  var repository = repofactory.create<customers>();

2、使用实例更新

customers customers = new customers { customerid = "yourc", companyname = "你的公司名", phone = "1390000000" };

repository.update().for(customers).go();

生成的sql语句

update [dbo].[customers]

set [customerid] = 'yourc', [companyname] = '你的公司名', [contactname] = null, [contacttitle] = null, [address] = null, [city] = null, [region] = null, [postalcode] = null, [country] = null, [phone] = '1390000000', [fax] = null where  [customerid] = 'yourc',  [companyname] = '你的公司名';

3、指定列值更新

repository.update()

                .set(m => m.companyname, "你的公司名100")

                .set(m => m.phone, "13900000023").where(m => m.customerid == "your1").go())

生成的sql语句

update [dbo].[customers]

set [companyname] = '你的公司名100', [phone] = '13900000023'

where ([dbo].[customers].[customerid] = 'your1');

三、删除记录

1、工厂一个实例仓储

  var repository = repofactory.create<customers>();

2、使用实例删除

 customers customers = new customers { customerid = "yourc", companyname = "你的公司名", phone = "1390000000" };

repository.delete().for(customers).go();

生成的sql语句

delete [dbo].[customers] where  [customerid] = 'yourc',  [companyname] = '你的公司名';

3、指定条件删除

repository.delete().where(m => m.customerid == "your1").go();

生成的sql语句

delete [dbo].[customers]

where ([dbo].[customers].[customerid] = 'your1');