博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dapper官方教程翻译9:Dapper方法之参数类型(转)
阅读量:5224 次
发布时间:2019-06-14

本文共 2690 字,大约阅读时间需要 8 分钟。

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.  
    }

 

转载于:https://www.cnblogs.com/LiZhongZhongY/p/10991672.html

你可能感兴趣的文章
HTML标签_1
查看>>
排序算法(转)
查看>>
windows自带的可生成各种数据库连接字符串工具打开方法
查看>>
Python命名规范
查看>>
滚动条
查看>>
程序员的自我修养九Windows下的动态链接
查看>>
细说WebSocket - Node篇
查看>>
Extjs控件之 grid打印功能
查看>>
枚举类型(不常用)递归
查看>>
minggw 安装
查看>>
Jquery操作cookie,实现简单的记住用户名的操作
查看>>
[BZOJ1196][HNOI2006]公路修建问题 二分答案+最小生成树
查看>>
【原创】大数据基础之Zookeeper(4)应用场景
查看>>
静态变量数组实现LRU算法
查看>>
中文系统 上传file的input显示英文
查看>>
比callback更简洁的链式执行promise
查看>>
android permission
查看>>
【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
查看>>
.net 文本框只允许输入XX,(正则表达式)
查看>>
[BSGS][哈希]luogu P3846 可爱的质数
查看>>