예제 #1
0
        static void Main(string[] args)
        {
            var con = new NpgsqlConnection(
                "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456789;Database=skychen");

            var user = new SysUser
            {
                CreateDatetime = DateTime.Now,
                Email          = "*****@*****.**",
                Mobile         = "18988563330",
                RealName       = "陈珙",
                SysUserid      = Guid.NewGuid().ToString("N"),
                UserName       = "******",
                UserStatus     = 1,
                UserType       = EUserType.Super,
                Password       = "******"
            };

            var insertResult = con.CommandSet <SysUser>().Insert(user);

            Console.WriteLine("Insert添加数{0}", insertResult);

            user.Email     = "*****@*****.**";
            user.SysUserid = Guid.NewGuid().ToString("N");
            var ifNotExistsResult = con.CommandSet <SysUser>().IfNotExists(a => a.Email == "*****@*****.**").Insert(user);

            Console.WriteLine("IfNotExists添加数{0}", ifNotExistsResult);

            user.SysUserid = Guid.NewGuid().ToString("N");
            var ifNotExistsResult2 = con.CommandSet <SysUser>().IfNotExists(a => a.Email == "*****@*****.**").Insert(user);

            Console.WriteLine("IfNotExists2添加数{0}", ifNotExistsResult2);

            var getResult = con.QuerySet <SysUser>().Get();

            getResult.Email = "*****@*****.**";
            var updateModelResult = con.CommandSet <SysUser>().Update(getResult);

            Console.WriteLine("Update添加数{0}", updateModelResult);

            var countResult = con.QuerySet <SysUser>().Where(a => a.Email == "*****@*****.**").Count();

            Console.WriteLine("Count数{0}", countResult);

            var toListResult = con.QuerySet <SysUser>().Where(a => a.Email == "*****@*****.**")
                               .OrderBy(a => a.CreateDatetime).Top(2).Select(a => a.Email).ToList();

            Console.WriteLine("ToList数{0}", toListResult.Count());

            var listResult2 = con.QuerySet <SysUser>().Where(a => a.Email == "*****@*****.**")
                              .OrderBy(a => a.CreateDatetime).Select(a => a.Email).PageList(2, 2);

            Console.WriteLine("PageList:{0}", listResult2.TotalPage);

            var updateResult4 = con.QuerySet <SysUser>().Sum(a => a.UserStatus);

            Console.WriteLine("Sum:{0}", updateResult4);

            var updateResult7 = con.QuerySet <SysUser>().Where(a => a.Email == "*****@*****.**")
                                .Top(2)
                                .Select(a => new SysUser {
                Email = a.Email
            })
                                .UpdateSelect(a => new SysUser {
                Email = "*****@*****.**"
            });

            Console.WriteLine("UpdateSelect:{0}", updateResult7.Count);

            var deleteResult = con.CommandSet <SysUser>().Delete();

            Console.WriteLine("Delete:{0}", deleteResult);

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            using (var transaction = con.BeginTransaction())
            {
                con.CommandSet <SysUser>(transaction).Insert(new SysUser
                {
                    CreateDatetime = DateTime.Now,
                    Email          = "*****@*****.**",
                    Mobile         = "11111111111",
                    RealName       = "陈珙",
                    SysUserid      = Guid.NewGuid().ToString("N"),
                    UserName       = "******",
                    UserStatus     = 1,
                    UserType       = EUserType.Super,
                    Password       = "******"
                });

                con.CommandSet <SysUser>(transaction).Insert(new SysUser
                {
                    CreateDatetime = DateTime.Now,
                    Email          = "*****@*****.**",
                    Mobile         = "22222222222",
                    RealName       = "陈珙",
                    SysUserid      = Guid.NewGuid().ToString("N"),
                    UserName       = "******",
                    UserStatus     = 1,
                    UserType       = EUserType.Super,
                    Password       = "******"
                });

                transaction.Commit();
            }

            con.Transaction(tc =>
            {
                var SysUserid1 = tc.QuerySet <SysUser>().Where(a => a.Mobile == "18988561110").Select(a => a.SysUserid).Get();

                var SysUserid2 = tc.QuerySet <SysUser>().Where(a => a.Mobile == "18988561111").Select(a => a.SysUserid).Get();

                tc.CommandSet <SysUser>().Where(a => a.SysUserid == SysUserid1).Delete();

                tc.CommandSet <SysUser>().Where(a => a.SysUserid == SysUserid2).Delete();

                tc.CommandSet <SysUser>().Insert(new SysUser
                {
                    CreateDatetime = DateTime.Now,
                    Email          = "*****@*****.**",
                    Mobile         = "13536059332",
                    RealName       = "大笨贞",
                    SysUserid      = Guid.NewGuid().ToString("N"),
                    UserName       = "******",
                    UserStatus     = 1,
                    UserType       = EUserType.Super,
                    Password       = "******"
                });
            }, ex =>
            {
                //do something
            });

            con.Dispose();
        }
예제 #2
0
        static void Main(string[] args)
        {
            var con = new NpgsqlConnection(
                "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456789;Database=skychen");

            var insertResult = con.CommandSet <SysUser>().Insert(new SysUser
            {
                CreateDatetime = DateTime.Now,
                Email          = "*****@*****.**",
                Mobile         = "18988561111",
                RealName       = "陈珙",
                SysUserid      = Guid.NewGuid().ToString("N"),
                UserName       = "******",
                UserStatus     = 1,
                UserType       = 1,
                Password       = "******"
            });

            Console.WriteLine("添加数{0}", insertResult);

            var insertResult2 = con.CommandSet <SysUser>().IfNotExists(a => a.Mobile == "18988563222").Insert(new SysUser
            {
                CreateDatetime = DateTime.Now,
                Email          = "*****@*****.**",
                Mobile         = "18988563222",
                RealName       = "陈珙",
                SysUserid      = Guid.NewGuid().ToString("N"),
                UserName       = "******",
                UserStatus     = 1,
                UserType       = 1,
                Password       = "******"
            });

            var countResult = con.QuerySet <SysUser>().Where(a => a.Email == "*****@*****.**").Count();

            Console.WriteLine("查询个数{0}", countResult);

            var getResult = con.QuerySet <SysUser>().Get();

            getResult.Email = "*****@*****.**";

            var updateModelResult = con.CommandSet <SysUser>().Update(getResult);

            Console.WriteLine("updateModelResult个数{0}", updateModelResult);

            var list = con.QuerySet <SysUser>().ToList();

            foreach (var item in list)
            {
                con.CommandSet <SysUser>().Where(a => a.SysUserid == item.SysUserid)
                .Update(a => new SysUser {
                    Email = "*****@*****.**"
                });
            }

            var listResult = con.QuerySet <SysUser>().OrderBy(a => a.CreateDatetime).Select(a => a.Email).ToList();

            var listResult2 = con.QuerySet <SysUser>().OrderBy(a => a.CreateDatetime).Top(2).Select(a => a.Email).ToList();

            var updateResult = con.CommandSet <SysUser>().Where(a => a.SysUserid == "487c9dac0c094a31a89fef1a98bc4204")
                               .Update(a => new SysUser {
                Email = "*****@*****.**"
            });

            getResult.Email = "*****@*****.**";
            var updateResult2 = con.CommandSet <SysUser>().Where(a => a.Email == "*****@*****.**").Update(getResult);

            var updateResult3 = con.QuerySet <SysUser>().Where(a => a.Email == "*****@*****.**").OrderBy(b => b.Email)
                                .Top(10).Select(a => a.Email).ToList();

            var updateResult8 = con.QuerySet <SysUser>().OrderBy(b => b.Email).Top(10).ToList();

            var updateResult4 = con.QuerySet <SysUser>().Sum(a => a.UserStatus);

            var updateResult6 = con.QuerySet <SysUser>().Where(a => a.Email == "*****@*****.**")
                                .OrderBy(a => a.CreateDatetime)
                                .Select(a => new SysUser {
                Email = a.Email, Mobile = a.Mobile, Password = a.Password
            }).PageList(1, 10);

            con.Transaction(tc =>
            {
                var SysUserid1 = tc.QuerySet <SysUser>().Where(a => a.Mobile == "18988561110").Select(a => a.SysUserid).Get();

                var SysUserid2 = tc.QuerySet <SysUser>().Where(a => a.Mobile == "18988561111").Select(a => a.SysUserid).Get();

                tc.CommandSet <SysUser>().Where(a => a.SysUserid == SysUserid1).Delete();

                tc.CommandSet <SysUser>().Where(a => a.SysUserid == SysUserid2).Delete();

                tc.CommandSet <SysUser>().Insert(new SysUser
                {
                    CreateDatetime = DateTime.Now,
                    Email          = "*****@*****.**",
                    Mobile         = "13536059332",
                    RealName       = "大笨贞",
                    SysUserid      = Guid.NewGuid().ToString("N"),
                    UserName       = "******",
                    UserStatus     = 1,
                    UserType       = 1,
                    Password       = "******"
                });
            });

            con.Dispose();
        }