[英]Data type mismatch in criteria expression error when trying to insert into Access database
I am trying to insert values entered by a user on a Winform into the following Access table: 我试图将用户在Winform上输入的值插入以下Access表中:
The C# code is: C#代码为:
OleDbCommand oleCmd = new OleDbCommand("INSERT INTO Projects (projectTitle,partyID,receiveDate,dueDate, projectTypeID, pages, "+
"lines, words, prepay, cost, settled,projectComment)"
+ " VALUES (@projectTitle,@partyID,@receiveDate,@dueDate,@projectTypeID,@pages, @lines, @words, @prepay, @cost, @settled, @projectComment)", conn);
PersianCalendar p=new PersianCalendar();
DateTime thisDate=DateTime.Now;
oleCmd.Parameters.Add("@projectTitle", OleDbType.VarChar).Value = txtProjectTitle.Text;
oleCmd.Parameters.Add("@partyID", OleDbType.Numeric).Value = Convert.ToInt32(comboProjectType.SelectedValue.ToString());
oleCmd.Parameters.AddWithValue("@receiveDate", string.Format( "{0}, {1}/{2}/{3} {4}:{5}:{6}",
p.GetDayOfWeek(thisDate), p.GetYear(thisDate),p.GetMonth(thisDate),p.GetDayOfMonth(thisDate), p.GetHour(thisDate),p.GetMinute(thisDate),p.GetSecond(thisDate))) ;
oleCmd.Parameters.Add("@dueDate", OleDbType.VarChar).Value = comboDay.Text + "/" + comboMonth.Text + "/" + comboYear.Text;
oleCmd.Parameters.Add("@projectTypeID", OleDbType.Numeric).Value = Convert.ToInt32(comboContractParty.SelectedValue.ToString());
oleCmd.Parameters.AddWithValue("@pages", Convert.ToInt32(txtPages.Text));
oleCmd.Parameters.AddWithValue("@lines", Convert.ToInt32(txtLines.Text));
oleCmd.Parameters.AddWithValue("@words", Convert.ToInt32(txtWords.Text));
oleCmd.Parameters.AddWithValue("@cost", Convert.ToDouble(txtWholeProjCost.Text));
oleCmd.Parameters.AddWithValue("@prepay", Convert.ToDouble(txtPrePay.Text));
oleCmd.Parameters.AddWithValue("@settled", chkSettled.CheckState);
oleCmd.Parameters.Add("@projectComment", OleDbType.VarChar).Value = txtComment.Text.ToString();
try
{
conn.Open();
oleCmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
The exception says: 异常说明:
Data type mismatch in criteria expression.
条件表达式中的数据类型不匹配。
I rechecked the types, but don't know where the error is raised from. 我重新检查了类型,但不知道从何处引发错误。
receiveDate
and dueDate
are defined as DateTime
in the Database but you are passing text (string) to the parameter: receiveDate
和dueDate
在数据库中定义为DateTime
,但是您正在将文本(字符串)传递给参数:
oleCmd.Parameters.AddWithValue("@receiveDate", string.Format(...
oleCmd.Parameters.Add("@dueDate", OleDbType.VarChar).Value = ...
Pass actual DateTime
vars and it should work. 传递实际的
DateTime
变量,它应该可以工作。 Since the date(s) are in pieces, I would create them first so they can be tested: 由于日期是分段的,因此我将首先创建它们,以便对其进行测试:
// why not use thisDate rather than chopping it up to recombine?
DateTime recDate = New DateTime(p.Getyear(thisDate)...);
// personally, I might use a DateTimePicker rather than 3 CBOs
DateTime dueDate = New DateTime(ConvertToInt32(comboYear.Text), ...
Then pass them as params. 然后将它们作为参数传递。 If you use
Add
specify OleDbType.Date
: 如果使用
Add
指定OleDbType.Date
:
oleCmd.Parameters.AddWithValue("@receiveDate", recDate);
...
oleCmd.Parameters.AddWithValue("@dueDate", dueDate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.