Unable to run update statement using Dapper (parameters error)

大兔子大兔子 提交于 2019-12-11 23:22:58

问题


I am trying to update a row using Dapper but I am having error on specifying parameters in my update statement

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

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

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.


回答1:


Workaround 1

What I did was to name parameters such that their order stays the same regardless of Dapper's alphabetical ordering.

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.




回答2:


Just try like this in your Repository.cs file

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 
               });
}


来源:https://stackoverflow.com/questions/21398513/unable-to-run-update-statement-using-dapper-parameters-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!