1

I use the following code to insert a record from one database to another but it doesn't work. I tried the query in MS-ACCESS 2007 and it works fine but it doesn't work when called programmatically from my C# code?

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();

When I take the the content of query_insert in ms access, it works fine

It throws

INSERT INTO syntax error exception in line cmd_insert.ExecuteNonQuery();

EDIT

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());
                    }



                }


        }

    }
}

EDIT

5
  • possible duplicate of insert into ms access database Commented Mar 25, 2013 at 17:12
  • Are you sure you are using IN correctly? Commented Mar 25, 2013 at 17:22
  • 1
    Where do you get that syntax FROM table IN filename ? Commented Mar 25, 2013 at 17:37
  • In Access can you use IN to point to a file location? Commented Mar 25, 2013 at 18:32
  • i try it and works perfect Commented Mar 25, 2013 at 18:47

4 Answers 4

1

I have found a different syntax in this Microsoft forum

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

so you could try this

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();

Tested with "Provider=Microsoft.ACE.OLEDB.12.0;"

However, with more research it is clear that the syntax error given is due to the presence of a reserved keyword CURRENT_DATE. This could be resolved encapsulating the field name with square brackets.

By the way, the IN syntax works as well once cured the problem of CURRENT_DATE.

Sign up to request clarification or add additional context in comments.

16 Comments

i use IN because the source database is different from the destination database
i'm using MS ACCESS 2007 .accdb i test this but still nothing happens thank you anyway for your courage
I have tested the code above (MS Access;.....) and it works with two different database and with ACE provider.
of course no, I have set up two different databases with a simple table and some data. Then I have done the transfer with the syntax like the one above. The important thing is: REMOVE the IN, add [MS Access;DATABASE=.......YOUR FILE.....;
my databases has different structure does this made the problem?
|
1

you are missing 'values' keyword in query_insert.

1 Comment

the values is the SELECT query
0

I'm not really familiar with ms access queries but I think you shouldn't use OleDbDataAdapter. The code should look similar to following:

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 Comments

did you know any other way to insert a record from one database table to other database table?
@user2183831, yes but I didn't use ms-access for a while. What I'm wandering is it is there any way to set db schema explicitly in access? Because it looks like you're inserting into the same table from which you query.
it just the same table name
0

Like others said, thats a SQL syntax problem. You are missing the VALUES keyword, look:

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

Where is your VALUES keyword?

3 Comments

@user2183831: First: try to write the VALUES keyword, like: INSERT INTO (COL1,COL2,COL3) VALUES (SELECT (COL1,COL2,COL3) FROM...) Second: try to use SqlParam in your Command object instead of concatenating strings. Will be better to you to find the errors source.
i still got the same exception
VALUES is the SELECT query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.