關於Dapper的使用筆記1

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   使用   sp   for   

********************************************************************1、Execute a query and map the results to a strongly typed ListNote: 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:
public class Dog{    public int? Age { get; set; }    public Guid Id { get; set; }    public string Name { get; set; }    public float? Weight { get; set; }     public int IgnoredProperty { get { return 1; } }}             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);

 

 ********************************************************************2、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); DbConnection con = this.GetConnection();return con.Query<t_So>("select * from dbo.t_so").ToList();//可以像上面一樣,支援參數化查詢,直接簡單!

 

********************************************************************3、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);

向資料據插入對象:

t_So so = new t_So();so.KeySeq = Guid.NewGuid();so.SoNo = this.sSoNo.Text;so.SoDate = GetValue.GetDateTime(this.sSoDate.Text);so.CustomerName = this.sCustomerName.Text;so.Remark = this.sRemark.Text; string strSQL = @"INSERT INTO dbo.t_so(KeySeq,SoNo,SoDate,CustomerName,Remark)VALUES(@KeySeq,@SoNo,@SoDate,@CustomerName,@Remark)";DbConnection con = this.GetConnection();//con.Execute(strSQL, new {KeySeq = so.KeySeq, SoNo = so.SoNo, SoDate = so.SoDate, CustomerName = so.CustomerName, Remark = so.Remark});con.Execute(strSQL, so);

********************************************************************

4、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 for some T. 這個可以批量插入的,不錯! 

關於Dapper的使用筆記1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.