/// <summary> /// Gets all MSSQL tables. /// </summary> private void GetAllMSSQLTables() { string connectionString = connectionString_MSSQL_Export; tables_insert = new Dictionary <string, string>(); tables_fields = new Dictionary <string, string>(); try { // Set the connection this.factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); cn = Factory.CreateConnection(); cn.ConnectionString = connectionString; cn.Open(); using (TransactionScope scope = new TransactionScope()) { cmd = this.CreateCommand(EXPORT_SELECT_ALL_TABLES, CommandType.Text); rdr = cmd.ExecuteReader(); if (rdr != null) { tables_insert = new Dictionary <string, string>(); while (rdr.Read()) { tables_insert.Add(rdr["tablename"].ToString(), rdr["insertscript"].ToString()); } } rdr.Close(); cmd = this.CreateCommand(EXPORT_SELECT_ALL_TABLES_FIELDS, CommandType.Text); rdr = cmd.ExecuteReader(); if (rdr != null) { tables_fields = new Dictionary <string, string>(); while (rdr.Read()) { tables_fields.Add(rdr["tablename"].ToString(), rdr["insertscript"].ToString()); } } rdr.Close(); scope.Complete(); } cn.Close(); } catch (Exception ex) { RetrievingTables.Invoke((Action)(() => this.RetrievingTables.Visible = false)); MessageBox.Show(ex.Message); hasErrors = true; } }
/// <summary> /// Gets all MYSQL tables. /// </summary> private void GetAllMYSQLTables() { string connectionString = connectionString_MYSQL_Export; string errorMsg = string.Empty; try { // Set the connection this.factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient"); cn = Factory.CreateConnection(); cn.ConnectionString = connectionString; // Open con and Begin Trans cn.Open(); trans = cn.BeginTransaction(); cmd = this.CreateCommand(EXPORT_SELECT_ALL_TABLES_MYSQL + "'" + cn.Database + "'", CommandType.Text); cmd.Transaction = trans; rdr = cmd.ExecuteReader(); if (rdr != null) { tables_mysql_db = new List <string>(); while (rdr.Read()) { tables_mysql_db.Add(rdr["TABLE_NAME"].ToString()); } } rdr.Close(); // Close con and Commit Trans trans.Commit(); cn.Close(); } catch (Exception ex) { hasErrors = true; if (trans != null) { trans.Rollback(); } RetrievingTables.Invoke((Action)(() => this.RetrievingTables.Visible = false)); MessageBox.Show(ex.Message); } }
/// <summary> /// Gets the mysql DB connection_ execute. /// </summary> private void GetMysqlDBConnection_Execute() { string connectionString = connectionString_MYSQL_Export + "Connection Timeout=180"; string errorMsg = string.Empty; StringBuilder item; try { if (queryToExecute != null && queryToExecute.Count > 0) { // Set the connection this.factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient"); cn = Factory.CreateConnection(); cn.ConnectionString = connectionString; isRunning = true; foreach (string table in queryToExecute.Keys) { // Open con and Begin Trans cn.Open(); trans = cn.BeginTransaction(); errorMsg = table; // Append to Log ListLog.Invoke((Action)(() => this.ListLog.AppendText(table + " Executing..." + Environment.NewLine))); ListLog.Invoke((Action)(() => this.ListLog.Focus())); queryToExecute.TryGetValue(table, out item); cmd = this.CreateCommand(item.ToString(), CommandType.Text); cmd.Transaction = trans; cmd.ExecuteNonQuery(); if (progressBar1.Value < maxProgress) { progressBar1.Invoke((Action)(() => this.progressBar1.Value += 1)); } // Append to Log ListLog.Invoke((Action)(() => this.ListLog.AppendText(table + " Completed." + Environment.NewLine))); ListLog.Invoke((Action)(() => this.ListLog.Focus())); // Close con and Commit Trans trans.Commit(); if (cn != null && cn.State == ConnectionState.Open) { cn.Close(); } } isRunning = false; progressBar1.Invoke((Action)(() => this.progressBar1.Value = maxProgress)); RetrievingTables.Invoke((Action)(() => this.RetrievingTables.Visible = false)); ListLog.Invoke((Action)(() => this.ListLog.AppendText((queryToExecute.Count - 2) + " tables copied successfully." + Environment.NewLine))); ListLog.Invoke((Action)(() => this.ListLog.Focus())); } } catch (Exception ex) { hasErrors = true; trans.Rollback(); this.ElapsedTime.Stop(); this.ElapsedTime.Enabled = false; //CancelBtn.Invoke((Action)(() => this.CancelBtn.Visible = false)); progressBar1.Invoke((Action)(() => this.progressBar1.Value = 0)); RetrievingTables.Invoke((Action)(() => this.RetrievingTables.Visible = false)); // Append to Log ListLog.Invoke((Action)(() => this.ListLog.AppendText("Execution aborted." + Environment.NewLine))); ListLog.Invoke((Action)(() => this.ListLog.Focus())); MessageBox.Show("Sorry, There was an error encountered, the error was on the " + errorMsg + " table." + Environment.NewLine + ex.Message); } }