0

我在不同的地方创建了以下代码Form

表格2

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 语句中给我一个语法错误?

4

1 回答 1

2

解决方案如下:

...
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);
}
...
于 2014-01-15T10:46:31.387 回答