zoukankan      html  css  js  c++  java
  • 星空雅梦

    Dapper官方教程翻译9:Dapper方法之参数类型

    1.匿名参数(常用)

    单个查询:

    1.  
      string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
    2.  
       
    3.  
      using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
    4.  
      {
    5.  
      var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});
    6.  
       
    7.  
      Console.WriteLine(affectedRows);
    8.  
       
    9.  
      // Only for see the Insert.
    10.  
      var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
    11.  
       
    12.  
      FiddleHelper.WriteTable(customer);
    13.  
      }

    批量查询:

    1.  
      string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
    2.  
       
    3.  
      using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
    4.  
      {
    5.  
      var affectedRows = connection.Execute(sql,
    6.  
      new[]
    7.  
      {
    8.  
      new {CustomerName = "John"},
    9.  
      new {CustomerName = "Andy"},
    10.  
      new {CustomerName = "Allan"}
    11.  
      }
    12.  
       
    13.  
      Console.WriteLine(affectedRows);
    14.  
      )

    2.动态参数(常用于存储过程)

    单个操作:

    1.  
      var sql = "EXEC Invoice_Insert";
    2.  
       
    3.  
      using (var connection = My.ConnectionFactory())
    4.  
      {
    5.  
      connection.Open();
    6.  
       
    7.  
      DynamicParameters parameter = new DynamicParameters();
    8.  
       
    9.  
      parameter.Add("@Kind", InvoiceKind.WebInvoice, DbType.Int32, ParameterDirection.Input);
    10.  
      parameter.Add("@Code", "Many_Insert_0", DbType.String, ParameterDirection.Input);
    11.  
      parameter.Add("@RowCount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
    12.  
       
    13.  
      connection.Execute(sql,
    14.  
      parameter,
    15.  
      commandType: CommandType.StoredProcedure);
    16.  
       
    17.  
      int rowCount = parameter.Get<int>("@RowCount");
    18.  
      }

    批量操作:

    1.  
      var sql = "EXEC Invoice_Insert";
    2.  
       
    3.  
      var parameters = new List<DynamicParameters>();
    4.  
       
    5.  
      for (var i = 0; i < 3; i++)
    6.  
      {
    7.  
      var p = new DynamicParameters();
    8.  
      p.Add("@Kind", InvoiceKind.WebInvoice, DbType.Int32, ParameterDirection.Input);
    9.  
      p.Add("@Code", "Many_Insert_" + (i + 1), DbType.String, ParameterDirection.Input);
    10.  
      p.Add("@RowCount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
    11.  
       
    12.  
      parameters.Add(p);
    13.  
      }
    14.  
       
    15.  
      using (var connection = My.ConnectionFactory())
    16.  
      {
    17.  
      connection.Open();
    18.  
       
    19.  
      connection.Execute(sql,
    20.  
      parameters,
    21.  
      commandType: CommandType.StoredProcedure
    22.  
      );
    23.  
       
    24.  
      var rowCount = parameters.Sum(x => x.Get<int>("@RowCount"));
    25.  
      }

    3.列表参数

    1.  
      var sql = "SELECT * FROM Invoice WHERE Kind IN @Kind;";
    2.  
       
    3.  
      using (var connection = My.ConnectionFactory())
    4.  
      {
    5.  
      connection.Open();
    6.  
       
    7.  
      var invoices = connection.Query<Invoice>(sql, new {Kind = new[] {InvoiceKind.StoreInvoice, InvoiceKind.WebInvoice}}).ToList();
    8.  
      }

    4.字符串参数

    1.  
      var sql = "SELECT * FROM Invoice WHERE Code = @Code;";
    2.  
       
    3.  
      using (var connection = My.ConnectionFactory())
    4.  
      {
    5.  
      connection.Open();
    6.  
       
    7.  
      var invoices = connection.Query<Invoice>(sql, new {Code = new DbString {Value = "Invoice_1", IsFixedLength = false, Length = 9, IsAnsi = true}}).ToList();
    8.  
       
    9.  
      My.Result.Show(invoices);
    10.  
      }
  • 相关阅读:
    tomcat部署web应用的4种方法
    Spring连接数据库的几种常用的方式
    Spring 的两个配置容器的讲解
    c3p0详细配置
    tomcat启动很慢的原因
    J2EE程序员应该要掌握的linux知识
    hibernate-mapping的各种属性配置
    在Eclipse添加Android兼容包( v4、v7 appcompat )
    微信抢红包软件-android
    Gradle sync failed: Failed to find Build Tools revision 26.0.2的解决办法
  • 原文地址:https://www.cnblogs.com/LiZhongZhongY/p/10991672.html
Copyright © 2011-2022 走看看