Dapper、MS Access、整数和 "no value given for one or more required parameters"

Dapper, MS Access, Integers and "no value given for one or more required parameters"

我第三次尝试插入下面的代码时一直收到错误 "no value given for one or more required parameters"。前两次尝试工作正常。我是 .NET 4.0 中的 运行,反对 VS 2012 中的 MS Access。无论我使用的是 2007 accdb 还是 2000-2003 mdb,这个问题似乎都是一致的。我正在使用以下代码进行测试:

using Dapper;

using System.Data.OleDb;

namespace DapperTest {
    class Program {
        static string accdb = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=W:\Projects\CSharp\DapperTest\DuhData.accdb;";
        static string mdb = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=W:\Projects\CSharp\DapperTest\DuhData.mdb";

        static void Main(string[] args) {
            var dis = new DisMember { FName = "Heza", LName = "Dufus", Initials = "", HighestGrade = 1 };

            using (OleDbConnection conn = new OleDbConnection(mdb)) {
                try {
                    conn.Execute("DROP TABLE DisMember;");
                }
                catch (System.Exception) { }

                conn.Execute("CREATE TABLE DisMember ( "
                            +     "FName CHAR, "
                            +     "Initials CHAR, "
                            +     "LName CHAR, "
                            +     "HighestGrade Number "
                            + "); "
                );

                // First attempt: This works in Dapper but does not include HighestGrade
                conn.Execute( "INSERT INTO DisMember(FName, Initials, LName) "
                            + "VALUES (@FName, @Initials, @LName);", dis
                            );

                // Second attempt: This works without Dapper and includes HighestGrade
                using (OleDbCommand cmd = conn.CreateCommand()) {
                    conn.Open();
                    cmd.CommandText = "INSERT INTO DisMember(FName, Initials, LName, HighestGrade) VALUES (?, ?, ?, ?); ";
                    cmd.Parameters.AddWithValue("FName", dis.FName);
                    cmd.Parameters.AddWithValue("Initials", dis.Initials);
                    cmd.Parameters.AddWithValue("LName", dis.LName);
                    cmd.Parameters.AddWithValue("HighestGrade", 1);
                    cmd.ExecuteNonQuery();
                }

                // Third attempt: This does not work with Dapper because of HighestGrade
                conn.Execute("INSERT INTO DisMember(FName, Initials, LName, HighestGrade) "
                            + "VALUES (@FName, @Initials, @LName, @HighestGrade);", dis
                            );
            }
        }
    }

    public class DisMember {
        public DisMember( ) { }
        public string FName { get; set; }
        public string LName { get; set; }
        public string Initials { get; set; }
        public int HighestGrade { get; set; }
    }
}

那么,这是 Dapper 中的错误吗?数据库?访问 2013?或者是其他东西?

非常感谢任何和所有线索。

好的,我明白了。预感我在其他地方发现的另一个答案表明 Dapper 中存在一个与 columns/parameters 的顺序有关的错误,我按字母顺序修改了代码,如下所示:

conn.Execute( "INSERT INTO DisMember(FName, HighestGrade, Initials, LName) "
            + "VALUES (@FName, @HighestGrade, @Initials, @LName);", dis
            );

并且有效。根据我找到的文章,这个bug已经在Dapper中被修复了,但是……显然没有。