標籤:get asc 需要 擴充 sel update 詳細 學生 end
前言
上一篇隨筆寫了Dapper的簡單的使用,這次寫一下Dapper.Extension的使用,它是Dapper的簡單的封裝擴充,可以通過執行個體化的對象賦值後進行增刪改的操作以及分頁,但是卻不能進行多表查詢的操作,這個需要自己來擴充吧。事務的操作的話和Dapper是一樣的。
1.擷取單個實體
這個比較簡單直接傳入id就可以擷取返回的對象,如下所示:
public Student ExtGet() { using(IDbConnection conn = new SqlConnection(sqlconnection)) { return conn.Get<Student>(1);//這裡只能傳id查詢,並且Student的欄位必須與表欄位匹配,多一個少一個都會報錯.所以擷取單個的對象感覺還是Dapper的原生語句好 //return conn.Get<Student>(" select id,score,courseId,studentId from Student where id = 1 ");//會報nvarchar轉換int報錯 } }
2.擷取多個實體List
具體代碼以及注釋如下所示:
public List<Student> ExtList() { using (IDbConnection conn = new SqlConnection(sqlconnection)) { IList<ISort> sortlist = new List<ISort>(); sortlist.Add(new Sort { PropertyName = "id", Ascending = false });//排序條件 IList<IPredicate> preList = new List<IPredicate>(); preList.Add(Predicates.Field<Student>(o => o.id, Operator.Eq, 2));//搜尋條件,Operator有很多種的類型如eq是等於、like相當於是sql的like用法還有Lt、Le等 BetweenValues betweenValues = new BetweenValues();//搜尋條件,between搜尋兩個值之間的資料 betweenValues.Value1 = 1; betweenValues.Value2 = 6; preList.Add(Predicates.Between<Student>(o => o.id, betweenValues)); var subPre = Predicates.Field<Student>(o => o.id, Operator.Eq, 3);//搜尋條件,判斷是否存在一個條件,後面的布爾參數如果是true表明不存在,false是存在 preList.Add(Predicates.Exists<Student>(subPre,true)); preList.Add(Predicates.Property<Student,Student>(o => o.id , Operator.Eq , t => t.id));//判斷兩個欄位的關係,比如是否相等、大於、小於等和Field差不多,並不能用於兩張表的欄位判斷 IPredicateGroup predGroup = Predicates.Group(GroupOperator.Or, preList.ToArray());//確認多個搜尋條件的串連方式AND 或者 OR var list = conn.GetList<Student>(predGroup, sortlist).ToList<Student>(); return list; //以上代碼產生的sql語句如下 //exec sp_executesql N‘SELECT [Student].[id], [Student].[name], [Student].[sex], [Student].[tel] FROM [Student] //WHERE (([Student].[id] = @id_0) //OR // ([Student].[id] BETWEEN @id_1 AND @id_2) //OR // (NOT EXISTS (SELECT 1 FROM [Student] WHERE ([Student].[id] = @id_3))) //OR //([Student].[id] = [Student].[id])) //ORDER BY [Student].[id] DESC‘,N‘@id_0 int,@id_1 int,@id_2 int,@id_3 int‘,@id_0=2,@id_1=1,@id_2=6,@id_3=3 } }
3.分頁
分頁具體和GetList的方法是一樣的,只是多了頁碼和每頁資料數量參數,具體代碼如下:
public List<Student> ExtPageList() { using (IDbConnection conn = new SqlConnection(sqlconnection)) { IList<ISort> sortlist = new List<ISort>(); sortlist.Add(new Sort { PropertyName = "id", Ascending = false });//排序條件 IList<IPredicate> preList = new List<IPredicate>(); preList.Add(Predicates.Field<Student>(o => o.id, Operator.Eq, 2)); IPredicateGroup predGroup = Predicates.Group(GroupOperator.Or, preList.ToArray()); return conn.GetPage<Student>(predGroup,sortlist,1,10).ToList(); } }
4.添加
/// <summary> /// 添加,只能單條添加 /// 通過傳入組裝好的類對象,就可以插入資料 /// 如果沒有資料會是NULL或者是資料庫欄位的預設值 /// </summary> public void ExtAdd() { using (IDbConnection conn = new SqlConnection(sqlconnection)) { Student student = new Student(); student.name = "測試學生"; student.sex = 1; //student.tel= "13222222222"; int id = conn.Insert<Student>(student);//返回自增主鍵id } }
5.更新
/// <summary> /// 更新,只能單條更新 /// 如果有欄位沒有賦值的情況下,會導致資料庫欄位更新為NULL, /// 所以需要注意,保證資料的完整 /// </summary> public void ExtUpdate() { using (IDbConnection conn = new SqlConnection(sqlconnection)) { Student student = new Student(); student.id = 1013; student.name = "測試學生"; student.sex = 1; //student.tel= "13222222222"; if (conn.Update<Student>(student)) { //成功 } else { //失敗 } } }
6.刪除
/// <summary> /// 刪除,只能單條刪除 /// 這邊可以通過傳入有id的實體或者Predicates對象來指定刪除的條件, /// Predicates的話和List那部分的操作是一樣的,可以參考ExtList種詳細的使用 /// </summary> public void ExtDel() { using (IDbConnection conn = new SqlConnection(sqlconnection)) { Student student = new Student(); student.id = 1020; var pre = Predicates.Field<Student>(o => o.id, Operator.Eq, 1017); if (conn.Delete<Student>(student))//或者conn.Delete<Student>(pre) { //成功 } else { //失敗 } } }
Dapper.Extension的基本使用