C# MySqL restore Code -
i'm tasked write c#.net code mysql backup , restore.
the backup taken server , restored on server b. code written, executes perfectly. restore not happening. put same command line c# code command prompt , execute it. restores there. not c# program. please me in identifying mistake i'm making.
static public void restore(string ip, string user, string password, string[] tbllist, string sourcedb, string targetdb) { try { string basecmd; basecmd = "/c mysql -h {0} -u {1} -p{2} {3} < {4}.sql"; foreach (string s in tbllist) { string db_tbl = sourcedb + "_" + s; string cmd = string.format(basecmd, ip, user, pass, targetdb, db_tbl); //cmd = cmd + " >error1234.txt"; system.threading.thread.sleep(1000); console.writeline(cmd); //system.diagnostics.process.start("cmd.exe", cmd); system.diagnostics.processstartinfo procstartinfo = new system.diagnostics.processstartinfo("cmd", cmd); procstartinfo.useshellexecute = false; procstartinfo.createnowindow = true; system.diagnostics.process proc = new system.diagnostics.process(); proc.startinfo = procstartinfo; proc.start(); //sendsuccesemail(); } } catch (exception ex) { console.writeline(ex); console.writeline("pause"); } }
i have written c# native library: mysqlbackup.net
project url: https://github.com/adriancs2/mysqlbackup.net
you can give try. below sample code accomplish task
string dbsource = "server=192.168.1.100;user=root;pwd=1234;database=db1;"; string dbtarget = "server=192.168.1.200;user=root;pwd=1234;database=db1;"; string sqldump = ""; // backup source database using (mysqlconnection conn = new mysqlconnection(dbsource)) { using (mysqlcommand cmd = new mysqlcommand()) { using (mysqlbackup mb = new mysqlbackup(cmd)) { conn.open(); cmd.connection = conn; sqldump = mb.exporttostring(); conn.close(); } } } // restore target database using (mysqlconnection conn = new mysqlconnection(dbtarget)) { using (mysqlcommand cmd = new mysqlcommand()) { using (mysqlbackup mb = new mysqlbackup(cmd)) { conn.open(); cmd.connection = conn; mb.importfromstring(sqldump); conn.close(); } } }
Comments
Post a Comment