[英]Troubleshooting “Data type mismatch in criteria expression.” during MS Access Insert.Into
我正在創建一個基本的客戶清單應用程序,將代碼從使用SQL Server轉換為使用MS Access(我不太了解)時,在嘗試執行此操作時遇到“數據類型不匹配”錯誤基本插入。
我在這里研究了幾個類似的問題,並仔細檢查了msdn語法指南,但找不到我編寫的腳本會產生該錯誤的原因。 我多次更改代碼以嘗試確保正確的數據類型(最后通過顯式鍵入並在后面添加值來完成下面的內容)。 實際上,我什至將字符串帶入並粘貼到MS Access(沒有空格和雙引號)中,並且似乎可以很好地使用給定的值。 在這一點上,我確實感到非常困惑,我想知道它是否可能是Oledb適配器的怪癖? 任何幫助,將不勝感激。 謝謝。
// SQL query defined elsewhere:
public static readonly string sqlAddCustomerNotes = "INSERT INTO CustomerNotes (Customer_ID, Notes, NotesDate) "
+ "VALUES(@Customer_ID, @Notes, @NotesDate);";
// end sql query
// data access function
public static void addNotes(int customerID, string notes, DateTime notesDate)
{
string query = Scripts.sqlAddCustomerNotes;
using (
OleDbCommand dbCommand = new OleDbCommand()
{
Connection = new OleDbConnection(ConnectionAccess.connString),
CommandType = CommandType.Text,
CommandText = query,
Parameters =
{
new OleDbParameter("@Customer_ID", OleDbType.Integer),
new OleDbParameter("@Notes", OleDbType.LongVarChar),
new OleDbParameter("@NotesDate", OleDbType.DBTimeStamp)
}
}) // end using parenthetical
{ // begin using scope
dbCommand.Parameters[0].Value = customerID;
dbCommand.Parameters[1].Value = notes;
dbCommand.Parameters[2].Value = notesDate;
foreach (OleDbParameter param in dbCommand.Parameters)
{ // replace ambiguous null values with explicit DBNulls.
if (param.Value == null)
{
param.Value = DBNull.Value;
}
}
dbCommand.Connection.Open();
int rowsAffected = dbCommand.ExecuteNonQuery();
dbCommand.Connection.Close();
Console.WriteLine($"Rows affected: {rowsAffected}");
}
} // end addCustomerNotes
/*
table "CustomerNotes" has the following columns:datatypes
CustomerNotes_ID: AutoNumber
Customer_ID: Number
Notes: Memo
NotesDate: Date/Time
CreateDate: Date/Time
test case (in code) was:
@Customer_ID = 5
@Notes = "customer might change last name to simpson."
@NotesDate = {6/26/2019 12:05:39 PM}
*/
它可能是一個日期 ,而不是一個時間戳 :
new OleDbParameter("@NotesDate", OleDbType.DBDate)
考慮到June7關於定界符的評論,似乎問題出在OleDbParameter
類型固有的某個問題上。 用SQL Server的術語來說,我確實希望使用DateTime
(而不是Date
),但是將其表示為DBTimeStamp
似乎會使Access無法識別它。
目前,我已將日期作為VarChar
發送,並允許Access對其進行轉換,但是其內部引擎認為合適。 感覺/似乎是錯誤的,但實際上可以解決問題。
Parameters =
{
new OleDbParameter("@Customer_ID", OleDbType.Integer),
new OleDbParameter("@Notes", OleDbType.LongVarChar),
new OleDbParameter("@NotesDate", OleDbType.VarChar)
}
編輯 :剛剛看到June7的最新評論,實際上, 另一個線程中有一個答案。 OleDbType.DBDate
不能滿足我的要求,但是OleDbType.Date
做到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.