0

I am using C# to upload data from Local MySQL database to remote MySQL database.

My current process is divided into 3 stages,

  1. Get data from local database and stored in the list.

  2. Upload the list to remote MySQL database.

  3. Move uploaded data at local database to a backup date table.

What bothers me more is the second stage because I use "foreach" to get each item in the list and upload to database. But if some error occurred during this foreach statement.(Etc. 3000 rows of data need to be uploaded, but network disconnect occurred at 1200.) Then, there will be some data uploaded finish and some data not. I need to drop the data table and upload again.

Is there any way to upload entire list instead of upload 1 row by 1 row? Or is there any way to handle about error occurred during uploading data to MySQL database?

A piece of my code

    MySqlConnection remoteConnect = new MySqlConnection(conStr);
    remoteConnect.open();

    foreach (var item in ActionLogDatasList)
    {
        switch (item.ActionID)
        {
            case 10053:     
            case 10054:     
            case 10101:     
                string strSQL3 = "INSERT INTO status_history VALUES('" + item.Time + "','" + item.ActionID + "','" + item.Message + "')";
                using (MySqlCommand uploadStatusHistory = new MySqlCommand(strSQL3, remoteConnect))
                {
                    uploadStatusHistory.ExecuteNonQuery();
                }
                break;
        }
    }
Shadow
  • 33,525
  • 10
  • 51
  • 64
TYS
  • 29
  • 8
  • 1
    In before someone else comments this: Please use [SqlCommand](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand?view=dotnet-plat-ext-5.0) with parameters to protect from Sql Injection – MindSwipe Nov 30 '20 at 08:30
  • https://dev.mysql.com/doc/connector-net/en/connector-net-programming-bulk-loader.html – mjwills Nov 30 '20 at 08:33

0 Answers0