【发布时间】:2015-02-17 16:32:29
【问题描述】:
我正在尝试将用户在 Winform 上输入的值插入到以下 Access 表中:
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;
}
异常说:
条件表达式中的数据类型不匹配。
我重新检查了类型,但不知道错误是从哪里引发的。
【问题讨论】:
-
如果您将@receiveDate 参数的当前(字符串)值暂时替换为
thisDate,您的代码是否有效? -
@GordThompson,仅使用 thisDate 后显示此错误:- ************ 异常文本 ************ ** System.FormatException:输入字符串的格式不正确。在 System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) 在 System.Convert.ToDouble(String value)
-
@GordThompson 上述错误是由于空输入,似乎可以通过为条目设置默认值 0 来解决。但是,我仍然收到“数据类型不匹配”错误。
-
如果使用
... VALUES (@projectTitle,@partyID,Date(),@dueDate, ...并注释掉创建@receiveDate 参数的语句会怎样? -
那是什么?我以前没见过这样的。括号中的 Date() 是什么?