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", 1, 2).ExecuteReturnLastId<int>(); var insert = Context().Insert("Worker") .Column("wName", "张三2") .Column("did", 1) .Column("gid", 2); int count2 = insert.ExecuteReturnLastId<int>(); //删除 int count3 = Context().Sql("delete Worker where wid = @wid").Parameter("wid", 7).Execute(); int count4 = Context().Delete("Worker").Where("wid", 8).Execute(); Worker _model2 = new Worker() { wid = 20, wname = "77", did = 10, gid = 11 }; int count8 = Context().Delete<Worker>("Worker", _model2) .Where(x => x.wid) .Execute(); //修改 var update = Context().Update("Worker") .Column("wName", "66") .Column("did", 66) .Where("wid", 13); int count5 = update.Execute(); Worker _model = new Worker() { wid = 17, wname = "77", did = 10, gid = 11 }; 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", 14) .QuerySingle(); int count6 = Context().Select<int>("count(1)") .From("Worker") .Where("wId = @wId").Parameter("wId", 14) .QuerySingle(); //分页 List<Worker> list = Context().Select<Worker>("*") .From("Worker") .Paging(2, 5) .OrderBy(" wid asc ") .QueryMany(); //反生异常,事务自动回滚 using (var context = Context().UseTransaction(true)) { context.Sql("insert Worker(wName,did,gid) values( @0,@1,@2)").Parameters("张三1123", 1, 2).ExecuteReturnLastId<int>(); context.Sql("update Product set Name = @0 where Id = @1") .Parameters("Product不存在数据库中", 1) .Execute(); context.Commit(); } //存储过程 var store = Context().StoredProcedure("ssss") .ParameterOut("UserID", DataTypes.Int32) .Parameter("UserName", "123"); 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; } } }
时间: 2024-10-12 12:53:23