-2

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;

textBox2.Text = query_insert.ToString();

dbConnDest.Close();

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

user2183831
  • 13
  • 1
  • 7

4 Answers4

3

I think you need to use

cmd_insert.executeNonQuery()
Crowlix
  • 1,269
  • 9
  • 19
1

Remove the comma after the last field name in the SELECT list.

"SELECT BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees"
HansUp
  • 95,961
  • 11
  • 77
  • 135
0

dAdapter.Update(); should do the trick

  • you created an insert statement but you didn't execute it. An example can be read here: [link](http://stackoverflow.com/questions/5874887/using-oledbdataadapter-and-dataset-to-update-access-mdb) You can leave out the DataTable related code if you want. –  Mar 25 '13 at 15:27
0

This seems suspect:

 " Where Branch_ID = " + textBox1.Text ;

Does textBox1 contain a numeric ID? Does the ID that is entered exist in the source database?

I would 1) do a check that the ID exists and warn the user if it doesn't, and 2) change the query to use paramters instead of concatenating SQL.

What would happen if your company opened a branch with the ID of

"1; DROP TABLE Branches"
D Stanley
  • 149,601
  • 11
  • 178
  • 240