如何使用dapper将参数按位置传递给存储过程?

时间:2016-12-27 22:04:46

标签: c# parameters dapper

# Redirect 'www' addresses to the non-www version with SSL.
server {                                                   
  listen 80;                                               
  server_name www.PLACEHOLDER_VHOST;                       
#  return 301 https://$host$request_uri;          //<------- commented out         
  return 301 http://$host$request_uri;            //<------- replaced with this         
}                                                          


  # Support for SSL
  #  if ($http_x_forwarded_proto = "http") {          //<------- commented out
  #    return 301 https://$host$request_uri;          //<------- commented out
  #  }                                                //<------- commented out

上面的方法有效,但我想传递参数而不是实际说这个=那个。以下方法不起作用我收到错误消息

  

其他信息:程序或功能&#39; AwesomeSauce&#39;期望参数&#39; @ TrainId&#39;,未提供

代码:

using (SqlConnection connection = new SqlConnection(ConnectionString))
{
   Train result = connection.Query<Train>("Trains.AwesomeSauce", new { TrainId =  "TRN001", CurrentTrack = "TR001"}, commandType: CommandType.StoredProcedure).FirstOrDefault<Train>();
}

我也尝试了下面的代码,但是得到了相同的

  

程序或功能&#39; AwesomeSauce&#39;期望参数&#39; @ TrainId&#39;,未提供

错误。

public void TestMethod5()
{
    using (SqlConnection connection = new SqlConnection(ConnectionString))
    {
        List<string> parameters = new List<string> { "TRN001", "TR001" };
        var list = connection.Query<Train>("Trains.Awesomesauce", new { parameters}, commandType: CommandType.StoredProcedure).FirstOrDefault<Train>();
    }
}

0 个答案:

没有答案