我使用以下代码将记录从一个数据库插入另一个数据库,但它不起作用。我在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();
当我在ms访问中获取query_insert
的内容时,它可以正常工作
它抛出
INSERT INTO语法错误异常 cmd_insert.ExecuteNonQuery();
修改
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());
}
}
}
}
}
修改
答案 0 :(得分:1)
你缺少价值观' query_insert中的关键字。
答案 1 :(得分:1)
我找到了不同的语法in this Microsoft forum
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;"
但是,通过更多的研究,很明显给出的语法错误是由于存在reserved keyword CURRENT_DATE。这可以解决用字括号封装字段名称。
顺便说一下,一旦解决了CURRENT_DATE的问题,IN语法也能正常工作。
答案 2 :(得分: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();
答案 3 :(得分:0)
INSERT INTO TABLENAME (COL1, COL2, COL2) VALUES (VAL1, VAL2, VAL3)
您的VALUES关键字在哪里?