简体   繁体   English

尝试插入Access数据库时条件表达式错误中的数据类型不匹配

[英]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: receiveDatedueDate在数据库中定义为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.

相关问题 C#访问:INSERT SQL语句的条件表达式错误中的数据类型不匹配 - C# Access: Data type mismatch in criteria expression error for INSERT SQL statement 条件表达式Oledb Access数据库中的数据类型不匹配 - Data type mismatch in criteria expression Oledb Access database 尝试从数据库中删除行时“条件表达式中的数据类型不匹配” - “Data type mismatch in criteria expression” when trying to delete row from database 条件表达式中的数据类型不匹配尝试添加行时发生异常 - Data type mismatch in criteria expression Exception when trying to add a row 查询访问数据库时条件表达式中的数据类型不匹配 - Data type mismatch in criteria expression when Query Access DB 准则表达中的数据类型不匹配错误(ms Access) - DATA TYPE MISMATCH error IN CRITERIA EXPRESSION (ms Access) 条件表达式Access 2013中的数据类型不匹配 - Data type mismatch in criteria expression Access 2013 条件表达式中的数据类型不匹配(Access DB) - Data type mismatch in criteria expression (Access DB) 条件表达式中的访问数据类型不匹配 - Access Data Type Mismatch in Criteria Expression 我正在尝试使用组合框和日期时间选择器将输入保存到ms Access数据库中,并说:条件表达式中的数据类型不匹配 - I am trying to save inputs using combo boxes and a date time picker to a ms access database and it says : Data type mismatch in criteria Expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM