关于Dapper的使用笔记1

********************************************************************
1、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:
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. 这个可以批量插入的,不错! 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。