Dapper操作

  public const string connString = "Data Source=.;Initial Catalog=test;User ID=sa;Password='qq'";

        private void dapperTest_Click(object sender, EventArgs e)
        {
            using (  SqlConnection conn = new SqlConnection(connString))
            {
                string query = "SELECT * FROM Sys_Users WHERE id = @id";
                List<User> list = conn.Query<User>(query, new { id = 100 }).ToList(); 
            }
        }

        private void dapperAdd_Click(object sender, EventArgs e)
        {
            int sqlResult = 0;
            User addModel = new User() { ID = 133, Age = 133, DepartmentID = 1, Name = "张三" };
            string sqlStr = "insert into sys_users(id,age,DepartmentID,name) values(@id,@age,@DepartmentID,@name)";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                sqlResult = conn.Execute(sqlStr,addModel);
               
            }
     
        }

        private void dapperUpdate_Click(object sender, EventArgs e)
        {
            int sqlResult = 0;
            User addModel = new User() { ID = 133, Name = "栗色" };
            string sqlStr = "update  sys_users set  name=@name where id=@id";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                sqlResult = conn.Execute(sqlStr, addModel);

            }
        }

        private void dapperDelete_Click(object sender, EventArgs e)
        {
            int sqlResult = 0;
            User addModel = new User() { ID = 133};
            string sqlStr = "delete from sys_users where id=@id";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                sqlResult = conn.Execute(sqlStr, addModel);

            }
        }
    

    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public int DepartmentID { get; set; }
        public string DepartmentID_Desc { get; set; }
    }
View Code

 

posted @ 2018-08-03 23:36  三醉  阅读(221)  评论(0编辑  收藏  举报