日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Dapper 介绍

發布時間:2023/12/13 综合教程 35 生活家
生活随笔 收集整理的這篇文章主要介紹了 Dapper 介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載:http://www.csdn123.com/html/itweb/20130918/125194_125199_125210.htm

.NET 輕量級 ORM 框架 - Dapper 介紹

Dapper簡單介紹:

Dapper is asingle fileyou can drop in to your project that will extend yourIDbConnectioninterface.

Dapper是一個輕型的開源ORM類,代碼就一個SqlMapper.cs文件,編譯后就40多K的一個很小的Dll. 官方資料:點擊這里

Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的數據庫,當然如果你知道原理也可以讓它支持Mongo db

Dapper的r支持多表并聯的對象。支持一對多 多對多的關系。并且沒侵入性,想用就用,不想用就不用。無XML無屬性。代碼以前怎么寫現在還怎么寫。

Dapper原理通過Emit反射IDataReader的序列隊列,來快速的得到和產生對象。性能提升了很多;(比采用常規的反射)

Dapper支持net2.0,3.0,3.5,4.0。不過就是要配置下。如果不知道如何配置查看我博客里的在2.0下使用3.5就可以了。

語法十分簡單。并且無須遷就數據庫的設計。

Dapper執行效率:

30W條數據,取其中的一個對象,和第一頁前15條數據,耗時0.0906879秒。這個速度超過Datable。

官方的測試代碼以及數據

Performance ofSELECTmapping over 500 iterations
- POCO serialization

Method Duration Remarks
Hand coded (using aSqlDataReader) 47ms
DapperExecuteMapperQuery<Post> 49ms
PetaPoco 52ms Can be faster
BLToolkit 80ms
SubSonicCodingHorror 107ms
NHibernate SQL 104ms
Linq 2 SQLExecuteQuery 181ms
Entity frameworkExecuteStoreQuery 631ms

Performance ofSELECTmapping over 500 iterations
- dynamic serialization

Method Duration Remarks
DapperExecuteMapperQuery
(dynamic)
48ms
Massive 52ms
Simple.Data 95ms

Performance ofSELECTmapping over 500 iterations
- typical usage

Method Duration Remarks
Linq 2 SQLCompiledQuery 81ms Not super typical involves complex code
NHibernate HQL 118ms
Linq 2 SQL 559ms
Entity framework 859ms
SubSonicActiveRecord.SingleOrDefault 3619ms

Dapper使用介紹:

如果你使用的是vs2012,可以使用NuGet來進行安裝,會自動添加引用,使用時寫入命名空間即可;

 

 using Dapper;

下面的代碼可以作為使用參考:

publicstaticreadonlystringsqlconnectionString
=
"Data
Source=xxx;Initial Catalog=Express;User ID=sa;Password=123"
;

publicstaticreadonlystringmysqlconnectionString
=
@"server=xxx;database=dddd;uid=xxx;pwd=123;charset='gbk'";
publicstaticSqlConnection
SqlConnection()
{
var
connection =
newSqlConnection(sqlconnectionString);
connection.Open();
returnconnection;
}
publicstaticMySqlConnection
MySqlConnection()
{
var
connection =
newMySqlConnection(mysqlconnectionString);
connection.Open();
returnconnection;

}

調用方法

SqlConnection connection = Program.SqlConnection();

獲得一個實體對象

var d = connection.Query<Dog>("select * from dog where id = 1",null).Single<Dog>();

獲得實體對象結合

var dd = connection.Query<Dog>("select * from dog where id < 10", null).ToList<Dog>();

插入數據

//動態參數
connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)",
new { @age = i,@name = Guid.NewGuid().ToString(), @Weight = i });   

//直接傳入實體
connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)",model);
Execute a query and map the results to a strongly typed List



Note: all extension methods assume the connection is already open, they will fail if the connection is closed.



public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)



Example usage:



publicclassDog

{

    publicint?Age{get;set;}

    publicGuidId{get;set;}

    publicstringName{get;set;}

    publicfloat?Weight{get;set;}



    publicintIgnoredProperty{get{return1;}}

}            

            

var guid =Guid.NewGuid();

var dog = connection.Query<Dog>("select Age = @Age, Id = @Id",new{Age=(int?)null,Id= guid });

            

dog.Count()

    .IsEqualTo(1);



dog.First().Age

    .IsNull();



dog.First().Id

    .IsEqualTo(guid);



Execute a query and map it to a list of dynamic objects



public static IEnumerable<dynamic> Query (this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)



This method will execute SQL and return a dynamic list.



Example usage:



 var rows = connection.Query("select 1 A, 2 B union all select 3, 4");



((int)rows[0].A)

   .IsEqualTo(1);



((int)rows[0].B)

   .IsEqualTo(2);



((int)rows[1].A)

   .IsEqualTo(3);



((int)rows[1].B)

    .IsEqualTo(4);



Execute a Command that returns no results



public static int Execute(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)



Example usage:



connection.Execute(@"

  set nocount on 

  create table #t(i int) 

  set nocount off 

  insert #t 

  select @a a union all select @b 

  set nocount on 

  drop table #t",new{a=1, b=2})

   .IsEqualTo(2);



Execute a Command multiple times



The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data)



Example usage:



connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",

    new[]{new{ a=1, b=1},new{ a=2, b=2},new{ a=3, b=3}}

  ).IsEqualTo(3);// 3 rows inserted: "1,1", "2,2" and "3,3"



This works for any parameter that implements IEnumerable<T> for some T.

var
dd = connection.Query<Dog>(
"select
* from dog where id < 10"
,null).ToList<Dog>();

總結

以上是生活随笔為你收集整理的Dapper 介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。