zoukankan      html  css  js  c++  java
  • 关于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. 这个可以批量插入的,不错! 

  • 相关阅读:
    AI:IPPR的数学表示-CNN稀疏结构进化(Mobile、xception、Shuffle、SE、Dilated、Deformable)
    基于视觉的机械手控制
    远程图形界面:VncServer与KDE桌面远程连接
    远程图形界面:使用putty+xmin远程登录ubuntu-kde
    CUDA 显存操作:CUDA支持的C++11
    C++11:using 的各种作用
    C++ 模板template和template
    Detectron:Pytorch-Caffe2-Detectron的一些跟进
    TF实战:(Mask R-CNN原理介绍与代码实现)-Chapter-8
    The type javax.servlet.http.HttpServletRequest cannot be resolved.
  • 原文地址:https://www.cnblogs.com/shi5588/p/4123843.html
Copyright © 2011-2022 走看看