0

I want to copy/duplicate a database using C# I tried in the following way but it gave me error saying check mysql syntax.

string connStr = "server=localhost;user=root;port=3306;";
            MySqlConnection conn = new MySqlConnection(connStr);
            MySqlCommand cmd;
            string s0;

            try
            {
                conn.Open();
                s0 = "mysqldump -h localhost -u root -p testdb | mysql -h localhost -u root -p `"+dbname+"`;";
                cmd = new MySqlCommand(s0, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            } 

No password for MySQL database. Can anyone tell me what's the issue in this code? Thanks.

Karthik Malla
  • 5,570
  • 12
  • 46
  • 89
  • possible duplicate of [this](http://stackoverflow.com/questions/935556/mysql-dump-by-query) – jomsk1e Apr 27 '14 at 15:07
  • @jomsk1e I think he intends to copy a database using C#, rather than using something like phpmyadmin. On that note, I find it odd that you wouldn't just create a dump of the database, create a new database, and use the exported SQL to copy it. – Silver Apr 27 '14 at 15:12

2 Answers2

0

You are trying to run an executable like it was a SQL command. It's not.

mysqldump is an executable that's present on your file system. To execute this app and make a backup of the db this way you have to use Shell. In C# you can use System.Diagnostics.Process to run this in a process.

Cyril Gupta
  • 13,505
  • 11
  • 64
  • 87
-1

If you code the call with just a -p parameter mysqldump will ask for a password, which of course you wont be able to enter as you are not running it from a command window.

You have to pass the password on the call i.e. -pAValidPassword (no space)

Of course this is a very insecure way of doing it as anybody wit access to your code now has access to the password!!

Documentation

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Andre
  • 1,044
  • 1
  • 11
  • 23