我有一个表单,用户可以在其中键入并从多个控件中进行选择,例如下拉列表,日期时间选择器,文本框,复选框等。在单击按钮事件中,我编写了以下过程以在MS Access数据库中插入新记录: 代码:
private void InsertNewRecord()
{
txtLastUpdated.Text = DateTime.Today.ToString();
using (var conn = new OleDbConnection(MTSConn))
{
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = @"Insert Into Incoming_T (DocFrom, CntrTeam, To, ReferenceNo, Subject, Review, Type, DocDate, RcvdDate, ProjectRef, DIMSNo, DIMSLink, SnrEng, MailStatus, Comments, LogRegID, RegisteredBy, UpdatedBy, LastUpdated, ForInf, ForReview, ForApp, ForComnt, ForHndlng, ForAct) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 100) { Value = cbxFrom.SelectedValue });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 20) { Value = cbxTo.SelectedValue });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 100) { Value = cbxTo.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 50) { Value = txtReference.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 200) { Value = txtSubject.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 1) { Value = cbxReview.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 10) { Value = cbxDocType.SelectedValue });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Date, 8) { Value = dtpDocDate.Value });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Date, 8) { Value = dtpRcvDate.Value });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 20) { Value = cbxProject.SelectedValue });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 15) { Value = txtDIMS.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 255) { Value = txtDIMLink.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 10) { Value = cbxSnrEngr.SelectedValue });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 20) { Value = cbxStatus.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 255) { Value = txtComments.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Integer, 4) { Value = Convert.ToInt32(txtRegID.Text) });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 50) { Value = txtRegBy.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.VarChar, 50) { Value = txtUpdatedBy.Text });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Date, 8) { Value = Convert.ToDateTime(txtLastUpdated.Text) });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Boolean, 1) { Value = Convert.ToBoolean(chkInf.Checked) });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Boolean, 1) { Value = Convert.ToBoolean(chkRw.Checked) });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Boolean, 1) { Value = Convert.ToBoolean(chkApp.Checked) });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Boolean, 1) { Value = Convert.ToBoolean(chkComm.Checked) });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Boolean, 1) { Value = Convert.ToBoolean(chkHandl.Checked) });
cmd.Parameters.Add(new OleDbParameter("?", OleDbType.Boolean, 1) { Value = Convert.ToBoolean(chkAct.Checked) });
try
{
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Recored Inserted Successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
将记录插入Access数据库时出现语法错误。我也尝试了这段代码,但还是遇到了同样的问题:
private void InsertNewRecord()
{
txtLastUpdated.Text = DateTime.Today.ToString();
using (OleDbConnection conn = new OleDbConnection(MTSConn))
{
using (OleDbCommand cmd = new OleDbCommand())
{
.
.
.
}
}
我在想什么吗?
答案 0 :(得分:1)
“类型”是reserved word in Access,因此您必须在查询中将其包装在方括号中:
Insert Into Incoming_T (DocFrom, CntrTeam, To, ReferenceNo, Subject, Review,
[Type], DocDate, RcvdDate, ProjectRef, DIMSNo, DIMSLink, SnrEng,
MailStatus, Comments, LogRegID, RegisteredBy, UpdatedBy, LastUpdated,
ForInf, ForReview, ForApp, ForComnt, ForHndlng, ForAct)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";