简体   繁体   English

无法使用Dapper运行更新语句(参数错误)

[英]Unable to run update statement using Dapper (parameters error)

I am trying to update a row using Dapper but I am having error on specifying parameters in my update statement 我试图使用Dapper更新一行,但我在更新语句中指定参数时出错

An exception of type 'System.InvalidOperationException' occurred in IBM.Data.DB2.iSeries.dll but was not handled in user code
Additional information: Not enough parameters specified.  The command
requires 3 parameter(s), but only 0 parameter(s) exist in the
parameter collection.

Repository.cs Repository.cs

public void Update(Movie movie)
{
      var sql = "UPDATE myDB.movies set title=?, genre=? where Id=?";
      db.Execute(sql, new 
               {   movie.Title, 
                   movie.Genre, 
                   movie.ID 
               });
}

Movie.cs Movie.cs

public class Movie
{
    public int ID { get; set; }
    [Required]
    public string Title { get; set; }

    [Required]
    [Display(Name="Release Date")]
    [DisplayFormat(DataFormatString="{0:dd/MM/yyyy}", ApplyFormatInEditMode=true)]
    public DateTime ReleaseDate { get; set; }

    [Required]
    public string Genre { get; set; }
    public decimal Price { get; set; }
}

I am using iDB2Connection to access an IBM ISeries Database. 我正在使用iDB2Connection访问IBM ISeries数据库。

Workaround 1 解决方法1

What I did was to name parameters such that their order stays the same regardless of Dapper's alphabetical ordering. 我所做的是命名参数,使得无论Dapper的字母顺序如何,它们的顺序保持不变。

public void Update(Movie movie)
{
  var sql = "UPDATE myDB.movies set title=@param1, genre=@param2 where ID=@param3";
  db.Execute(sql, new { param1 = movie.Title, param2 = movie.Genre, param3 = movie.ID });
}

If someone knows a better solution. 如果有人知道更好的解决方案。 please post in this thread. 请在这个帖子中发帖。

Just try like this in your Repository.cs file 只需在Repository.cs文件中尝试这样

public void Update(Movie movie)
{
      var sql = "UPDATE myDB.movies set title=:Title, genre=:Genre where Id=:ID";
      db.Execute(sql, new 
               {  
                  Title = movie.Title, 
                  Genre = movie.Genre, 
                  ID = movie.ID 
               });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM