views:

29

answers:

2

How can I dump some of the tables from my mysql database into an sql file in C#? Is there a class which does this?

UPDATE: just wanted to mention to DO NOT USE mysqldump, because this application will be installed on many computers and the mysql folder could be on different places.

+1  A: 

Dotconnect for mysql might have this feature, but I don't know about the free version.

Otherwise you could just invoke the mysqldump utility and do something like this:

public void DumpMySQLDb(string user, string password, string database, string outputFile) {
  var commandLine = string.Format("mysqldump --user={1}--password={2} --hex-blob --databases {3}",
     user, password, database)
  var process = new Process();
  process.StartInfo = new ProcessStartInfo {
      FileName = "cmd",
      Arguments = string.Format( "/c \"{0}\" > {1}", commandLine, outputFile )
  };
  process.Start();
}
Bertrand Marron
can the mysqldump be used without the path in front?
Ervin
Of course, if it's in your `PATH` environment variable.
Bertrand Marron
but it wont't. Anyway thanks for the answer. Appreciate it. I think I'm going to build a dump class, read the tables and data and write to file.
Ervin
A: 

I built up the sql string table by talbe in the end.

Ervin