无法使用Dapper运行更新语句(参数错误) [英] Unable to run update statement using Dapper (parameters error)

查看:363
本文介绍了无法使用Dapper运行更新语句(参数错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Dapper更新一行,但是在更新语句中指定参数时出现错误

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

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

我正在使用iDB2Connection访问IBM ISeries数据库.

I am using iDB2Connection to access an IBM ISeries Database.

推荐答案

解决方法1

我所做的就是命名参数,以便无论Dapper的字母顺序如何,它们的顺序都保持不变.

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.

这篇关于无法使用Dapper运行更新语句(参数错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆