Dapper SimpleCRUD Demo

时间:2021-12-16 05:09:39

using System;
using TestDapper.Common;
using Dapper;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace TestDapper.ConsoleHelper
{
    [Table("User")]
    public class User
    {
        [Column("user_id"), Key]
        public int UserId { get; set; }
        [Column("user_name")]
        public string UserName { get; set; }
        public string password { get; set; }
        [Column("mobile_phone")]
        public string MobilePhone { get; set; }
        [Editable(false)]
        public string NameAndPhone { get { return string.Format("{0}:{1}", UserName, MobilePhone); } }
        [ReadOnly(true), Column("created")]
        public DateTime Created { get; set; }
        [ReadOnly(true), Column("modified")]
        public DateTime Modified { get; set; }
    }
    class Program
    {

        private static string sqlconnStr = "Data Source=(local);Initial Catalog=XD;User Id=sa;Password=skyjiushiwo;pooling=true;max pool size=512";

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.SQLServer);
            using (var db = new SqlConnection(sqlconnStr))
            {
                var user1 = db.Get<User>(1000);
                if (user1 != null) {
                    Console.WriteLine($"{user1.UserId} {user1.UserName} {user1.password} {user1.MobilePhone}");
                }

                var firstCount = db.RecordCount<User>();
                Console.WriteLine($"第一次找到{firstCount}个用户");

                //var UserId = db.Insert(new User
                //{
                //    UserName = "userxxx",
                //    password = "passwordxxx",
                //    MobilePhone = "13666666000",
                //});

                //var userX = new User()
                //{
                //    UserId = 11,
                //    UserName = "userxx",
                //    password = "passwordxx",
                //    MobilePhone = "13666666666"
                //};
                //db.Update(userX);

                //db.Delete<User>(12);
                //db.DeleteList<User>(new { UserName = "userxxx" });
                //db.DeleteList<User>("Where user_id > @id", new { id = 12 });

                var second = db.RecordCount<User>();
                Console.WriteLine($"第二次找到{second}个用户");

                var third = db.RecordCount<User>("where user_name like @UserName", new { UserName = "%user%" });
                Console.WriteLine($"第三次找到{third}个用户");



                List<User> users = db.GetList<User>("where user_name like @UserName", new { UserName = "%user%" }).ToList();
                foreach (var user in users) {
                    Console.WriteLine($"{user.UserId} {user.UserName} {user.password} {user.MobilePhone}");
                }
                Console.WriteLine("-");
                List<User> users2 = db.GetListPaged<User>(2, 4,"where user_name like @UserName", "user_name asc", new { UserName = "%user%" }).ToList();
                foreach (var user in users2)
                {
                    Console.WriteLine($"{user.UserId} {user.UserName} {user.password} {user.MobilePhone}");
                    Console.WriteLine($"{user.NameAndPhone}");
                }
            }


        }
    }
}