我一直在第三次嘗試插入下面的代碼時收到錯誤「給出的一個或多個必需參數沒有值」。前兩次嘗試正常工作。我在.NET 4.0中運行,針對VS 2012中的MS Access。而且,無論使用2007 accdb還是2000-2003 mdb,此問題似乎都是一致的。我使用下面的代碼來測試:Dapper,MS Access,Integers和「沒有給出一個或多個所需參數的值」
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中的一個錯誤嗎? OLEDB? Access 2013?或者是其他東西?
任何和所有線索非常感謝。
讓HighestGrade成爲數據庫中的一個整數? –
這是一個很好的建議,但這是(最終)反對現有數據。我必須像現在這樣處理它。 :) – EDanaII
然後讓HighestGrade成爲你的類定義中的一個浮點數!或者執行一些轉換。 –