1

我使用以下代码将记录从一个数据库插入到另一个数据库,但它不起作用。我尝试了查询,MS-ACCESS 2007它工作正常,但是从我的代码以编程方式调用时它不起作用C#

string query_insert = "INSERT INTO Questionnaires_Table(BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees) "
    + "SELECT BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees "
    + "FROM Questionnaires_Table IN '" + dialog.FileName + "' Where Branch_ID = " + textBox1.Text ;

dbConnDest.Open();


   OleDbDataAdapter dAdapter = new OleDbDataAdapter();
   OleDbCommand cmd_insert = new OleDbCommand(query_insert, dbConnDest);

   dAdapter.InsertCommand = cmd_insert;
   cmd_insert.ExecuteNonQuery();

dbConnDest.Close();

当我获取query_insertin ms 访问的内容时,它工作正常

它抛出

cmd_insert.ExecuteNonQuery() 行中的 INSERT INTO 语法错误异常;

编辑

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Title = "select database";



            if ((dialog.ShowDialog() == DialogResult.OK) && (textBox1.Text == ""))
            {
                    MessageBox.Show("insert reference year", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {




                    OleDbConnection dbConnDest;
                    dbConnDest = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\SystemA.accdb;Persist Security Info=False;");

                    try
                    {



                        string query_insert = "INSERT INTO Questionnaires_Table(BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees) "
                                               + "SELECT BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees "
                                               + "FROM Questionnaires_Table1 IN '" + dialog.FileName + "' Where ReferenceYear = " + textBox1.Text + ";";




                        dbConnDest.Open();



                        OleDbCommand cmd_insert = new OleDbCommand(query_insert, dbConnDest);

                        try
                        {
                            cmd_insert.ExecuteNonQuery();
                        }
                        catch (Exception g)
                        {
                            MessageBox.Show(g.ToString());
                        }



                        textBox2.Text = query_insert.ToString();

                        dbConnDest.Close();
                    }
                    catch (Exception h)
                    {
                        MessageBox.Show(h.ToString());
                    }



                }


        }

    }
}

编辑

4

4 回答 4

1

您在 query_insert 中缺少“值”关键字。

于 2013-03-25T17:12:28.480 回答
1

在这个 Microsoft 论坛中发现了不同的语法

  INSERT INTO [AccessTable] SELECT * FROM [MS Access;DATABASE=D:\My Documents\db2.mdb].[Table2]

所以你可以试试这个

string query_insert = "INSERT INTO Questionnaires_Table " +
    "(BranchName,Factor,Region,Branch_ID,[Current_Date],No_Employees) " +
    "SELECT BranchName,Factor,Region,Branch_ID,[Current_Date],No_Employees " +
    "FROM [MS Access;DATABASE=" + dialog.FileName  + "].Questionnaires_Table " +
    "Where Branch_ID = @branch";

dbConnDest.Open();
OleDbCommand cmd_insert = new OleDbCommand(query_insert, dbConnDest);
cmd_insert.Parameters.AddWithValue("@branch", textBox1.Text);
cmd.ExecuteNonQuery();

经测试"Provider=Microsoft.ACE.OLEDB.12.0;"

然而,随着更多的研究,很明显给出的语法错误是由于保留关键字CURRENT_DATE 的存在。这可以通过用方括号封装字段名称来解决。

顺便说一句,一旦解决了 CURRENT_DATE 的问题,IN 语法也能正常工作。

于 2013-03-25T17:42:39.703 回答
0

就像其他人说的那样,那是一个 SQL 语法问题。您缺少 VALUES 关键字,请查看:

INSERT INTO TABLENAME (COL1, COL2, COL2) VALUES (VAL1, VAL2, VAL3)

您的 VALUES 关键字在哪里?

于 2013-03-25T18:29:22.440 回答
0

我不太熟悉 ms 访问查询,但我认为您不应该使用 OleDbDataAdapter。代码应类似于以下内容:

string query_insert = "INSERT INTO Questionnaires_Table(BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees) "
    + "SELECT BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees "
    + "FROM Questionnaires_Table IN '" + dialog.FileName + "' Where Branch_ID = " + textBox1.Text ;

dbConnDest.Open();

OleDbCommand cmd_insert = new OleDbCommand(query_insert, dbConnDest);
cmd_insert.ExecuteNonQuery();

dbConnDest.Close();
于 2013-03-25T17:14:20.353 回答