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

  • 相关阅读:
    vue 传参动态
    a href="tel" 拨打电话
    vue中rem的转换
    请求接口的封装
    http request 请求拦截器,有token值则配置上token值
    node溢出
    vue菜单切换
    vue的table切换
    vue页面初始化
    [论文笔记] Legacy Application Migration to the Cloud: Practicability and Methodology (SERVICES, 2012)
  • 原文地址:https://www.cnblogs.com/shi5588/p/4123843.html
Copyright © 2020-2023  润新知