FluentData(微型ORM)

时间:2022-04-24 12:31:15
using FluentData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test
{ class Program
{
static void Main(string[] args)
{
//新增
int count1 = Context().Sql("insert Worker(wName,did,gid) values( @0,@1,@2)").Parameters("张三1", , ).ExecuteReturnLastId<int>(); var insert = Context().Insert("Worker")
.Column("wName", "张三2")
.Column("did", )
.Column("gid", ); int count2 = insert.ExecuteReturnLastId<int>(); //删除
int count3 = Context().Sql("delete Worker where wid = @wid").Parameter("wid", ).Execute();
int count4 = Context().Delete("Worker").Where("wid", ).Execute();
Worker _model2 = new Worker()
{
wid = ,
wname = "",
did = ,
gid =
}; int count8 = Context().Delete<Worker>("Worker", _model2)
.Where(x => x.wid)
.Execute(); //修改
var update = Context().Update("Worker")
.Column("wName", "")
.Column("did", )
.Where("wid", );
int count5 = update.Execute(); Worker _model = new Worker()
{
wid = ,
wname = "",
did = ,
gid =
};
int count7 = Context().Update<Worker>("Worker", _model)
.AutoMap(x => x.wid)
.Where(x => x.wid)
.Execute(); //查询
//单个对象
Worker worker = Context().Select<Worker>("*")
.From("Worker")
.Where("wId = @wId").Parameter("wId", )
.QuerySingle(); int count6 = Context().Select<int>("count(1)")
.From("Worker")
.Where("wId = @wId").Parameter("wId", )
.QuerySingle(); //分页
List<Worker> list = Context().Select<Worker>("*")
.From("Worker")
.Paging(, )
.OrderBy(" wid asc ")
.QueryMany(); //反生异常,事务自动回滚
using (var context = Context().UseTransaction(true))
{ context.Sql("insert Worker(wName,did,gid) values( @0,@1,@2)").Parameters("张三1123", , ).ExecuteReturnLastId<int>();
context.Sql("update Product set Name = @0 where Id = @1")
.Parameters("Product不存在数据库中", )
.Execute();
context.Commit();
} //存储过程
var store = Context().StoredProcedure("ssss")
.ParameterOut("UserID", DataTypes.Int32)
.Parameter("UserName", ""); var result = store.QueryMany<Worker>();
int count9 = store.ParameterValue<int>("UserID");
} public static IDbContext Context()
{
var context = new DbContext().ConnectionString("server=.;uid=sa;pwd=123;database=Test;", new SqlServerProvider());
return context;
}
} public class Worker
{
public int wid { get; set; }
public string wname { get; set; }
public int did { get; set; }
public int gid { get; set; }
} }