码迷,mamicode.com
首页 > 移动开发 > 详细

Dapper SimpleCRUD Demo

时间:2019-11-28 19:30:36      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:asc   dial   dem   ini   editable   delete   return   sky   initial   

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}");
                }
            }


        }
    }
}

  

Dapper SimpleCRUD Demo

标签:asc   dial   dem   ini   editable   delete   return   sky   initial   

原文地址:https://www.cnblogs.com/hack0573/p/11951830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!