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