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

  • 相关阅读:
    tcp_wrapper过滤
    cobbler无人值守批量安装Linux系统
    PXE+kickstart无人值守安装CentOS 7
    kickstart文件详解
    SHELL脚本--shell数组基础
    SHELL脚本--管道和重定向基础
    第4章 DHCP服务
    第3章 NFS基本应用
    man sm-notify(sm-notify命令中文手册)
    man statd(rpc.statd中文手册)
  • 原文地址:https://www.cnblogs.com/shi5588/p/4123843.html
Copyright © 2011-2022 走看看