在 Access 数据库中插入新行 [重复]
Posted
技术标签:
【中文标题】在 Access 数据库中插入新行 [重复]【英文标题】:Insert new row in a Access Database [duplicate] 【发布时间】:2014-01-15 10:22:32 【问题描述】:我在不同的Form
中创建了以下代码:
Form2
private DataTable dataTable;
internal void ReadTable(ref DataTable dt)
dataTable = dt;
private void button1_Click(object sender, EventArgs e)
DataRow dataRow = dataTable.NewRow();
foreach (ListViewItem item in listView1.Items)
dataRow[item.SubItems[1].Text] = item.SubItems[item.SubItems.Count - 1].Text;
dataTable.Rows.Add(dataRow);
表格1
private void button1_Click(object sender, EventArgs e)
using (Form2 form = new Form2())
form.ReadTable(ref dataTable);
form.ShowDialog();
using (OleDbConnection oledbConnection = new OleDbConnection(connection))
oledbConnection.Open();
string query = "SELECT * FROM Student";
using (OleDbCommand oledbCommand = new OleDbCommand(query, oledbConnection))
using (OleDbDataAdapter oledbDataAdapter = new OleDbDataAdapter(oledbCommand))
using (OleDbCommandBuilder oledbCommandBuilder = new OleDbCommandBuilder(oledbDataAdapter))
oledbDataAdapter.DeleteCommand = oledbCommandBuilder.GetDeleteCommand(true);
oledbDataAdapter.InsertCommand = oledbCommandBuilder.GetInsertCommand(true);
oledbDataAdapter.UpdateCommand = oledbCommandBuilder.GetUpdateCommand(true);
oledbDataAdapter.Update(dataTable);
oledbConnection.Close();
为什么在 INSERT INTO 语句中给我一个语法错误?
【问题讨论】:
语法错误的任何细节? 不,它只是告诉我存在语法错误 :( 我试图查看 INSERT INTO 命令的代码,结果是正确的 您能编辑您的问题并粘贴生成的插入命令的 SQL 吗? this answer 有帮助吗? 戈德·汤普森你太棒了:D 非常感谢!!! 【参考方案1】:解决方法如下:
...
using (OleDbCommandBuilder oledbCommandBuilder = new OleDbCommandBuilder(oledbDataAdapter))
oledbCommandBuilder.QuotePrefix = "[";
oledbCommandBuilder.QuoteSuffix = "]";
oledbDataAdapter.DeleteCommand = oledbCommandBuilder.GetDeleteCommand(true);
oledbDataAdapter.InsertCommand = oledbCommandBuilder.GetInsertCommand(true);
oledbDataAdapter.UpdateCommand = oledbCommandBuilder.GetUpdateCommand(true);
oledbDataAdapter.Update(dataTable);
...
【讨论】:
以上是关于在 Access 数据库中插入新行 [重复]的主要内容,如果未能解决你的问题,请参考以下文章