Ejemplo n.º 1
0
 public bool Restore(String Host, String Attivita)
 {
     try
     {
         using (MySqlConnection conn = new MySqlConnection(Host))
         {
             using (MySqlCommand cmd = new MySqlCommand())
             {
                 using (MySqlBackup mb = new MySqlBackup(cmd))
                 {
                     cmd.Connection = conn;
                     conn.Open();
                     if (Attivita == "Bar")
                     {
                         mb.ImportFromString(Properties.Resources.Bar);
                     }
                     else if (Attivita == "Pub")
                     {
                         mb.ImportFromString(Properties.Resources.Pub);
                     }
                     else if (Attivita == "Void")
                     {
                         mb.ImportFromString(Properties.Resources.VOID_DB);
                     }
                     conn.Close();
                 }
             }
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        void Step3()
        {
            MessageBox.Show("Running Step 3...");

            using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        conn.Open();
                        cmd.Connection = conn;

                        cmd.CommandText = string.Format("drop database if exists `{0}`", txtDatabaseName.Text);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = string.Format("create database `{0}`;", txtDatabaseName.Text);
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = string.Format("use `{0}`", txtDatabaseName.Text);
                        cmd.ExecuteNonQuery();

                        mb.ImportFromString(txtStep2.Text);

                        conn.Close();
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public void ImportString(string querystring, MySqlBackup.importComplete onDone, EventHandler <MySqlBackup.ExceptionEventArgs> onError, MySqlBackup.importProgressChange OnProgress)
 {
     Task.Factory.StartNew(() =>
     {
         try
         {
             using (MySqlCommand cmd = new MySqlCommand())
             {
                 cmd.Connection = new MySqlConnection(mArgs.GetConnectionString());
                 cmd.Connection.Open();
                 using (MySqlBackup bck = new MySqlBackup(cmd))
                 {
                     bck.ImportProgressChanged += OnProgress;
                     bck.ImportCompleted       += onDone;
                     bck.ImportFromString(querystring);
                 }
             }
         }
         catch (MySqlException ex)
         {
             onError(this, new MySqlBackup.ExceptionEventArgs()
             {
                 ex = ex
             });
         }
         catch (Exception ex)
         {
             onError(this, new MySqlBackup.ExceptionEventArgs()
             {
                 ex = ex
             });
         }
     });
 }
Ejemplo n.º 4
0
        private void btImport_Click(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();

                            mb.ImportFromString(textBox1.Text);

                            conn.Close();
                        }
                    }
                }
                MessageBox.Show("Import completed.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 5
0
        private void btRestore_Click(object sender, EventArgs e)
        {
            string sql = txtSql.Text;

            MySqlBackup mb = null;

            try
            {
                using (MySqlConnection conn = new MySqlConnection(txtConStr.Text))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        mb = new MySqlBackup(cmd);
                        conn.Open();
                        cmd.Connection = conn;

                        mb.ImportFromString(sql);

                        conn.Close();
                    }
                }

                MessageBox.Show("Done");
            }
            catch (Exception ex)
            {
                var er = mb.LastError;
                MessageBox.Show(ex.ToString() + "\r\n\r\n" + er.ToString());
            }
        }
Ejemplo n.º 6
0
        private void btTest_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog of = new OpenFileDialog();
                if (of.ShowDialog() != DialogResult.OK)
                {
                    lbResult.Text = string.Empty;
                    return;
                }

                // Step 1: Compute the hash value of the bytes of the file
                byte[] ba1   = File.ReadAllBytes(of.FileName);
                string hash1 = BitConverter.ToString(System.Security.Cryptography.SHA1Managed.Create().ComputeHash(ba1)).Replace("-", string.Empty).ToLower();

                lbHash1.Text    = hash1;
                lbHash2.Text    = string.Empty;
                lbResult.Text   = string.Empty;
                lbDumpSize.Text = string.Empty;

                this.Refresh();

                // Step 2: Insert blob into database
                using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        conn.Open();
                        cmd.Connection  = conn;
                        cmd.CommandText = "delete from testblob";
                        cmd.ExecuteNonQuery();

                        cmd.CommandText = "insert into testblob(blobdata)values(@blobdata);";
                        cmd.Parameters.AddWithValue("@blobdata", ba1);
                        cmd.ExecuteNonQuery();

                        conn.Close();
                    }
                }

                if (MessageBox.Show("File inserted into database. Do you want to continue?", "Next", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    lbResult.Text = "Operation Cancelled";
                    return;
                }

                // Step 3: Export the database
                string sqlDump = string.Empty;
                using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();

                            mb.ExportInfo.AddDropTable         = true;
                            mb.ExportInfo.ExportTableStructure = true;
                            mb.ExportInfo.ExportRows           = true;

                            if (dropBlobExportMode.SelectedIndex < 1)
                            {
                                mb.ExportInfo.BlobExportMode = BlobDataExportMode.HexString;
                            }
                            else
                            {
                                mb.ExportInfo.BlobExportMode = BlobDataExportMode.BinaryChar;
                                mb.ExportInfo.BlobExportModeForBinaryStringAllow = true;
                            }

                            sqlDump = mb.ExportToString();

                            conn.Close();
                        }
                    }
                }
                lbDumpSize.Text = sqlDump.Length.ToString();
                this.Refresh();

                // Step 4: Reimport the database
                using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();

                            mb.ImportFromString(sqlDump);

                            conn.Close();
                        }
                    }
                }

                // Step 5: Get the blob data from database
                byte[] ba2 = null;
                using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        cmd.Connection = conn;
                        conn.Open();

                        cmd.CommandText = "select blobdata from testblob;";
                        ba2             = (byte[])cmd.ExecuteScalar();

                        conn.Close();
                    }
                }

                // Step 6: Compute the hash value of the bytes from database
                string hash2 = BitConverter.ToString(System.Security.Cryptography.SHA1Managed.Create().ComputeHash(ba2)).Replace("-", string.Empty).ToLower();

                lbHash2.Text = hash2;

                if (hash1 == hash2)
                {
                    lbResult.Text      = "SUCCESS";
                    lbResult.ForeColor = Color.DarkGreen;
                }
                else
                {
                    lbResult.Text      = "FAIL!";
                    lbResult.ForeColor = Color.Red;
                }

                this.Refresh();

                if (MessageBox.Show("test completed. Do you want to write the output bytes to file?", "completed", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    SaveFileDialog sf = new SaveFileDialog();
                    sf.FileName = of.FileName;
                    if (sf.ShowDialog() == DialogResult.OK)
                    {
                        File.WriteAllBytes(sf.FileName, ba2);
                    }
                }
            }
            catch (Exception ex)
            {
                lbResult.Text      = ex.Message;
                lbResult.ForeColor = Color.Red;

                MessageBox.Show("Error: " + ex.ToString());
            }
        }
Ejemplo n.º 7
0
        void RunTest(string sql)
        {
            txtError.Text = "";
            txtLastErrorSqlSyntax.Text = "";
            this.Refresh();

            try
            {
                string oldlocation = File.ReadAllText(cacheLogFilePath);

                if (oldlocation != txtLogFilePath.Text)
                {
                    File.WriteAllText(cacheLogFilePath, txtLogFilePath.Text);
                }
            }
            catch
            {
                File.WriteAllText(cacheLogFilePath, txtLogFilePath.Text);
            }

            try
            {
                File.Delete(txtLogFilePath.Text);
            }
            catch { }

            using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        conn.Open();

                        try
                        {
                            cmd.Connection = conn;

                            mb.ImportInfo.IgnoreSqlError = cbIgnoreSqlError.Checked;
                            mb.ImportInfo.ErrorLogFile   = txtLogFilePath.Text;
                            mb.ImportFromString(sql);
                        }
                        catch
                        { }

                        conn.Close();

                        try
                        {
                            if (txtLogFilePath.Text.Length > 0)
                            {
                                txtError.Text = File.ReadAllText(txtLogFilePath.Text);
                            }
                            else
                            {
                                txtError.Text = mb.LastError.ToString();
                            }
                        }
                        catch { }

                        txtLastErrorSqlSyntax.Text = mb.LastErrorSQL;
                    }
                }
            }

            MessageBox.Show("Task ended.");
        }
Ejemplo n.º 8
0
        private void btStart_Click(object sender, EventArgs e)
        {
            txtError.Text = "";
            txtLastErrorSqlSyntax.Text = "";
            this.Refresh();

            try
            {
                string oldlocation = File.ReadAllText(cacheLogFilePath);

                if (oldlocation != txtLogFilePath.Text)
                {
                    File.WriteAllText(cacheLogFilePath, txtLogFilePath.Text);
                }
            }
            catch
            {
                File.WriteAllText(cacheLogFilePath, txtLogFilePath.Text);
            }

            try
            {
                File.Delete(txtLogFilePath.Text);
            }
            catch { }

            string data = GetDumpContent();

            using (MySqlConnection conn = new MySqlConnection(Program.ConnectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        conn.Open();

                        try
                        {
                            cmd.Connection = conn;

                            mb.ImportInfo.IgnoreSqlError = cbIgnoreSqlError.Checked;
                            mb.ImportInfo.ErrorLogFile   = txtLogFilePath.Text;
                            mb.ImportFromString(data);
                        }
                        catch
                        { }

                        conn.Close();

                        try
                        {
                            if (txtLogFilePath.Text.Length > 0)
                            {
                                txtError.Text = File.ReadAllText(txtLogFilePath.Text);
                            }
                            else
                            {
                                txtError.Text = mb.LastError.ToString();
                            }
                        }
                        catch { }

                        txtLastErrorSqlSyntax.Text = mb.LastErrorSQL;
                    }
                }
            }
        }