使用 C# windows 窗体向 Ms Access 数据库插入新记录时出现语法错误
Posted
技术标签:
【中文标题】使用 C# windows 窗体向 Ms Access 数据库插入新记录时出现语法错误【英文标题】:Syntax error in inserting new record to Ms Access Database using C# windows form 【发布时间】:2019-01-23 10:50:41 【问题描述】:我有一个表单,用户可以在其中键入和选择多个控件,例如下拉列表、日期时间选择器、文本框、复选框等。在按钮单击事件中,我编写了以下过程来插入 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())
.
.
.
我的表单如下所示:
我错过了什么吗?
【问题讨论】:
【参考方案1】:“Type”是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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
【讨论】:
如果您将收到的实际错误的文本包含在问题中,也许会有所帮助。 谢谢 stuartd,但它显示“连接未建立”。我尝试在添加参数之前打开连接,但仍然有问题。将 [Type] 字段包含在括号内对 this 错误没有影响。 在你的问题中你说你收到了一个语法错误。 YES,但是当我将 using (var cmd = conn.CreateCommand()) 更改为 using (OleDbConnection conn = new OleDbConnection(MTSConn)) 时,错误消息更改为“连接属性尚未初始化" 看起来你得到了那个错误,因为你没有将连接分配给命令对象——如果你使用cmd = new OleDbCommand()
而不是cmd = conn.CreateCommand()
,那么你必须告诉命令什么连接使用 - cmd.Connection = conn
.以上是关于使用 C# windows 窗体向 Ms Access 数据库插入新记录时出现语法错误的主要内容,如果未能解决你的问题,请参考以下文章
具有 Ms 访问权限的 C# Windows 窗体应用程序无法插入数据