简体   繁体   中英

Copy entire database from one server to another in .NET

I am tasked with writing a data transfer utility and one requirement is that I copy an entire MySQL database from one server to another. The user will simply click a button when they want the database transfer to occur.

I am a little inexperienced with databases, but I worked with them enough to know how to do what I need to do. What is the quickest way of doing this?

My original idea was do this:

  1. Get a list of all tables
  2. Foreach table, get all contents of every table and store them in a DataTable in memory
  3. Backup all old tables to a CSV file
  4. Truncate all old tables
  5. Insert the new DataTables into the appropriate database on the appropriate server

Is there a better, more efficient way to do this?

DataTables take up quite a bit of memory. Be careful with this approach, as database size grows, so does the likelyhood that this method will fill up your memory. I would look at using the mysql command line to backup / restore and call those commands from your code.

Using Process.Start Method =>

Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM