轻量ORM-SqlRepoEx (四)INSERT、UPDATE、DELETE 语句
*本文中所用类声明见上一篇博文《轻量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');
上一篇: c#继承类实例
下一篇: protected(C# 参考)