How to properly copy a row of data from one Access database to another using C#? -
so, have launch_data.mdb , out_data.mdb. data table (any) in launch_data loaded in form datagridview. user click on row, , click on "move to" button. program needs move row launch_data.mdb out_data.mdb. both databases have same table names , structures.
code:
private void button3_click(object sender, eventargs e) { con1.open(); using (var cmd = new oledbcommand()) { cmd.connection = con1; cmd.commandtype = system.data.commandtype.text; cmd.commandtext = @"insert " + treeview1.selectednode.name + " in " + system.io.directory.getcurrentdirectory() + @"\out_data.mdb " + @"select * " + treeview1.selectednode.name + " [added at:] ('" + selectedkey + "')"; cmd.executenonquery(); } con1.close(); }
and con 1 is:
oledbconnection con1 = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=" + system.io.directory.getcurrentdirectory() + @"\launch_data.mdb");
when ran code, commandtext contained: "cmd.commandtext = "insert h11 in c:\\users\\uidg3149\\desktop\\ppap area management\\ppap area management\\bin\\debug\\out_data.mdb select * h11 [added at:] ('3/24/2014 2:31:15 pm')""
me looks ok (should insert should should, gives me "syntax error in insert statement."
. pretty new c# , it's first time try such operation.
you need 2 connections. connection read data first database , populate datagridview, second connection write data out_data database.
create parameters insert query getting values selected row doing this.
cmd.parameters.addwithvalue("?", datagridview1.selectedrows[0].cells[0]);
access doesn't support named parameters have add them in order use them in query. insert statement might bit this:
"insert table (col1, col2, col3) values (?, ?, ?)"
Comments
Post a Comment