其他分享
首页 > 其他分享> > Dapper官方教程翻译9:Dapper方法之参数类型(转)

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

作者:互联网

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

2019年02月28日 10:54:45  阅读数:84  

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

 

标签:翻译,CustomerName,教程,Add,connection,sql,var,new,Dapper
来源: https://www.cnblogs.com/LiZhongZhongY/p/10991672.html