Click here to Skip to main content
15,896,063 members

Restore MySQL DB through C#

iamgun10a asked:

Open original thread
I try to backup and restore MySQL database from C# using process.

First BackUP:
the command to backup database is :
-u USER -pPASS --databases DATABASE -r > path/to/file.sql
Here is my C# code:
C#
Process.Start(@"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe", ("-u user -p***** --databases DB -r \"" + path + "\"";

Look at the difference, in my code is not ">", becuse it wont work with it. If I put this char to code, it just show me usage of mysqldump in win cmd. Without it, this code work good.

Now i want to restore database.
the command to restore database is :
-u USER -pPASS DATABASE < path/to/file.sql
Here is my c# codes :

1#
C#
Process.Start(@"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe", ("-u user -p**** < \"" + path + "\""));


2#
C#
Process myProcess = new Process();
myProcess.StartInfo.FileName = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.WorkingDirectory = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\";
myProcess.StartInfo.Arguments = "-u user -p**** PIEx < + "\"" + path + "\"";
myProcess.StartInfo.RedirectStandardInput = false;
myProcess.StartInfo.RedirectStandardOutput = false;
myProcess.Start();

myProcess.WaitForExit();
myProcess.Close();


Both solutions dont work. I think it is becuse of this char ">", the same problem like backup, but in this case, removing it dont work. The result is same, it write down mysql usages in cmd console.

If I execute this commands manually in cmd (move to mysql bin, then run mysql with arguments), it work fine. I try redirect output/input/error to stream readers/writers - not work. Try to UseShellExecute = true - not work. Nothing works.

Help me guys. Thx
Tags: C#, MySQL, Process

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900