예제 #1
0
        private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            _overallProgressCounter = 0;
            int totalErrors = 0;

            while (_tablesForMigration.Count > 0)
            {
                FileInfo tableInfo = _tablesForMigration.Dequeue();
                string   dbFile    = tableInfo.FullName;

                #region -- migrate table to mysql ---

                try
                {
                    DataTable table = MigrationHelper.OpenFile(dbFile);
                    if (table == null)
                    {
                        throw new Exception(string.Format("Unable to open {0}.", dbFile));
                    }
                    int totalRecords = table.Rows.Count;
                    int counter      = 0;
                    foreach (DataRow dataRow in table.Rows)
                    {
                        var paramList   = new List <SqlParameter>();
                        var fieldNames  = new List <string>();
                        var fieldParams = new List <string>();

                        foreach (DataColumn column in table.Columns)
                        {
                            object defaultFieldValue = MigrationHelper.GetDefaultFieldValue(dataRow[column],
                                                                                            column.DataType);
                            if (defaultFieldValue == null)
                            {
                                continue;
                            }

                            string fieldParam = "?" + column.ColumnName;
                            paramList.Add(new SqlParameter(fieldParam, defaultFieldValue));
                            fieldNames.Add("`" + column.ColumnName + "`");
                            fieldParams.Add(fieldParam);
                        }

                        string tableName  = Path.GetFileNameWithoutExtension(dbFile);
                        var    sqlBuilder = new StringBuilder();
                        sqlBuilder.AppendLine(string.Format("INSERT INTO `{0}`", tableName));
                        sqlBuilder.AppendLine(string.Format("({0})", string.Join(", ", fieldNames)));
                        sqlBuilder.AppendLine(string.Format("VALUES ({0})", string.Join(", ", fieldParams)));
                        DatabaseController.ExecuteInsertQuery(sqlBuilder.ToString(), paramList.ToArray());
                        counter++;
                        var percent = (int)(counter * (100m / totalRecords));
                        _worker.ReportProgress(percent);
                    }
                    _overallProgressCounter++;
                }
                catch (Exception exception)
                {
                    totalErrors++;
                    Logger.ExceptionLogger(this, exception);
                    MessageWindow.ShowAlertMessage(exception.Message);
                    _tablesForMigration.Enqueue(tableInfo);
                    if (totalErrors > 10)
                    {
                        _worker.CancelAsync();
                    }
                }

                #endregion -- end migrate table to mysql --
            }

            UpdateJournalVoucherDocumentNumber();
        }