private void button_Restore_Click(object sender, EventArgs e)
        {
            if (!Program.SourceFileExists())
            {
                return;
            }

            Exception error = null;

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

                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            //mb.ImportInfo.EnableEncryption = cbImEnableEncryption.Checked;
                            //mb.ImportInfo.EncryptionPassword = txtImPwd.Text;
                            mb.ImportInfo.IgnoreSqlError = cbImIgnoreSqlErrors.Checked;
                            //mb.ImportInfo.TargetDatabase = txtImTargetDatabase.Text;
                            //mb.ImportInfo.DatabaseDefaultCharSet = txtImDefaultCharSet.Text;
                            mb.ImportInfo.ErrorLogFile = txtImErrorLogFile.Text;

                            mb.ImportFromFile(Program.TargetFile);

                            error = mb.LastError;
                        }

                        conn.Close();
                    }
                }

                if (error == null)
                {
                    MessageBox.Show("Finished.");
                }
                else
                {
                    MessageBox.Show("Finished with errors." + Environment.NewLine + Environment.NewLine + error.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
        private void BtnConfirm_Click(object sender, EventArgs e)
        {
            string constring = string.Format("server=localhost;user={0};pwd={1};database={2};", Config.Instance.DatabaseUser, Config.Instance.DatabasePassword, Config.Instance.DatabaseName);

            if (radioBtnBackup.Checked)
            {
                // TODO 密码不能寫死,也不能寫在程式码內
                using (MySqlConnection conn = new MySqlConnection(constring))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();
                            mb.ExportToFile(txtBoxPath.Text);
                            conn.Close();
                        }
                    }
                }
                MessageBox.Show(strings.DBBackupFinish);//"数据库备份完成!");
                //StrCmd = "mysqldump -uroot -p123456 icmdb.net > ";
            }
            else
            {
                using (MySqlConnection conn = new MySqlConnection(constring))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();
                            mb.ImportFromFile(txtBoxPath.Text);
                            conn.Close();
                        }
                    }
                }
                MessageBox.Show(strings.DBRecoverFinish);//"数据库還原完成!");
            }
            //if(GetMysqlPath(MysqlPath) == false)
            //{
            //    MessageBox.Show("Error~ Mysql not exist!");
            //}
            //else
            //{

            //}
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Import database from SQL script from filename specified
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool importDatabase(string filename)
        {
            var success = true;

            using (var cmd = new MySqlCommand())
            {
                using (var mb = new MySqlBackup(cmd))
                {
                    cmd.Connection = conn;
                    mb.ImportFromFile(filename);
                }
            }

            return(success);
        }
 public void RestoreDatabase(string fileLocation)
 {
     try
     {
         using (var context = new DbContext())
         {
             var mysqlBackup = new MySqlBackup();
             mysqlBackup.Command = (MySqlCommand)context.Conn.CreateCommand();
             mysqlBackup.ImportFromFile(fileLocation);
         }
     }
     catch (MySqlException ex)
     {
         throw ex;
     }
 }
Exemplo n.º 5
0
 public void RestoreDB(string connstring, string fileTarget)
 {
     using (MySqlConnection conn = new MySqlConnection(connstring))
     {
         using (MySqlCommand cmd = new MySqlCommand())
         {
             using (MySqlBackup mb = new MySqlBackup(cmd))
             {
                 cmd.Connection = conn;
                 conn.Open();
                 mb.ImportFromFile(fileTarget);
                 conn.Close();
             }
         }
     }
 }
Exemplo n.º 6
0
 internal static void Restore(string database, string backupFile)
 {
     using (var conn = new MySqlConnection(ConnectionString(database)))
     {
         using (var cmd = new MySqlCommand())
         {
             using (var mb = new MySqlBackup(cmd))
             {
                 cmd.Connection = conn;
                 conn.Open();
                 mb.ImportFromFile(backupFile);
                 conn.Close();
             }
         }
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// 还原数据库
        /// </summary>
        /// <param name="strPath">指定还原文件***.sql的绝对路径</param>
        /// <param name="dbName">还原到指定数据库</param>
        /// <returns></returns>
        public static bool RestoreDB(string strPath, string dbName)
        {
            bool isImport = false;

            try
            {
                MySqlConnection myconn = new MySqlConnection(ConnectString);
                if (myconn.State == ConnectionState.Closed)
                {
                    myconn.Open();
                }
                try
                {
                    if (string.IsNullOrEmpty(strPath))
                    {
                        return(isImport);
                    }
                    using (MySqlCommand cmmd = new MySqlCommand())
                    {
                        using (MySqlBackup backCmd = new MySqlBackup(cmmd))
                        {
                            cmmd.Connection     = myconn;
                            cmmd.CommandTimeout = 3600;
                            backCmd.ImportInfo.TargetDatabase         = dbName;//前提条件 当前 myconn 中的用户有建库等系列权限
                            backCmd.ImportInfo.DatabaseDefaultCharSet = "utf8";
                            backCmd.ImportFromFile(strPath);
                            isImport = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    if (myconn.State == ConnectionState.Open)
                    {
                        myconn.Close();
                        myconn.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(isImport);
        }
        //Import database
        private void importDB_Btn_Click(object sender, EventArgs e)
        {
            String         database = null;
            OpenFileDialog ofd      = new OpenFileDialog();

            ofd.Title  = "Select directory to import SQL";
            ofd.Filter = "SQL Files (.sql)|*.sql";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                database = ofd.FileName;
            }

            String          connStr    = "Server=localhost; Port=3306; Uid=root; Pwd=admin;";
            MySqlConnection connection = new MySqlConnection(connStr);

            try
            {
                MySqlCommand cmd    = new MySqlCommand();
                MySqlBackup  backup = new MySqlBackup(cmd);
                cmd.Connection = connection;
                connection.Open();
                backup.ImportInfo.TargetDatabase         = splitDir(database);
                backup.ImportInfo.DatabaseDefaultCharSet = "utf8";
                backup.ImportFromFile(database);
                MessageBox.Show("Backup has been imported");
            }
            catch (Exception)
            {
                if (database == null)
                {
                    MessageBox.Show("Please select a database to export");
                }
            }
            finally
            {
                if (connection == null)
                {
                    //Do nothing !
                }
                else if (connection.State == ConnectionState.Open)
                {
                    connection.Close();
                    listBox1.Items.Clear();
                    showDBList();
                }
            }
        }
Exemplo n.º 9
0
        private static int ImportCharacters(CommandLineOptions.ImportOptions opts)
        {
            var importFileName = System.Configuration.ConfigurationManager.AppSettings["DefaultOutputFile"];

            if (!String.IsNullOrEmpty(opts.ImportFileName))
            {
                importFileName = opts.ImportFileName;
            }

            Logger.Debug($"Command Line Options : {importFileName}");

            var connection = new MySqlConnection(ConnectionString);

            try
            {
                Logger.Info($"Connecting to {ConnectionString}. Exporting to {importFileName}");
                Logger.Info("Start Character Import");


                using (MySqlConnection conn = new MySqlConnection(ConnectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();
                            mb.ImportFromFile(importFileName);
                            conn.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Logger.Error($"Exception : {e.Message}");
                throw;
            }
            finally
            {
                connection.Close();
                Logger.Info("Done Character Export");
            }

            return(0);
        }
Exemplo n.º 10
0
        void DoRestore()
        {
            string[] files = Directory.GetFiles(folder);

            DateTime timeProcessStart = DateTime.Now;

            using (MySqlConnection conn = new MySqlConnection(constr))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    conn.Open();
                    cmd.Connection = conn;

                    foreach (string file in files)
                    {
                        string db = Path.GetFileNameWithoutExtension(file);

                        DateTime dateStart = DateTime.Now;

                        string appendText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  Restore " + db + "....";
                        bw.ReportProgress(0, appendText);

                        cmd.CommandText = "create database if not exists `" + db + "`";
                        cmd.ExecuteNonQuery();

                        cmd.CommandText = $"use `{db}`";
                        cmd.ExecuteNonQuery();

                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            mb.ImportFromFile(file);
                        }

                        DateTime dateEnd = DateTime.Now;

                        var timeElapsed = dateEnd - dateStart;

                        appendText = $" completed ({timeElapsed.Hours} h {timeElapsed.Minutes} m {timeElapsed.Seconds} s {timeElapsed.Milliseconds} ms)\r\n";
                        bw.ReportProgress(0, appendText);

                        count++;
                    }

                    conn.Close();
                }
            }
        }
Exemplo n.º 11
0
 public bool restore(string file)
 {
     using (conn)
     {
         using (MySqlCommand cmd = new MySqlCommand())
         {
             using (MySqlBackup mb = new MySqlBackup(cmd))
             {
                 cmd.Connection = conn;
                 conn.Open();
                 mb.ImportFromFile(file);
                 conn.Close();
                 return(true);
             }
         }
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// 还原数据库
        /// </summary>
        /// <param name="path">指定还原文件***.sql的绝对路径</param>
        /// <param name="dbName">还原到指定数据库</param>
        /// <returns></returns>
        public static bool RestoreDb(string path, string dbName)
        {
            bool isSuccess = false;

            try
            {
                MutiDBOperate mutiDB = Appsettings.app <MutiDBOperate>("DBS")
                                       .Where(i => i.Enabled && i.ConnId == "WMBLOG_MYSQL").First();
                MySqlConnection myconn = new MySqlConnection(mutiDB.Connection);
                if (myconn.State == ConnectionState.Closed)
                {
                    myconn.Open();
                }
                try
                {
                    using (MySqlCommand cmmd = new MySqlCommand())
                    {
                        using (MySqlBackup backCmd = new MySqlBackup(cmmd))
                        {
                            cmmd.Connection     = myconn;
                            cmmd.CommandTimeout = 3600;
                            //backCmd.ImportInfo.TargetDatabase = dbName;//前提条件 当前 myconn 中的用户有建库等系列权限
                            //backCmd.ImportInfo.DatabaseDefaultCharSet = "utf8";
                            backCmd.ImportFromFile(path);
                            isSuccess = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    //Console.WriteLine($"BackupDB_备份数据库异常 sql:{cmdText}. {ex.Message}", "MYSQLIMPL");
                }
                finally
                {
                    if (myconn.State == ConnectionState.Open)
                    {
                        myconn.Close();
                        myconn.Dispose();
                    }
                }
            }
            catch (Exception)
            {
            }
            return(isSuccess);
        }
Exemplo n.º 13
0
 public static void GeriYukle(string path)
 {
     using (MySqlConnection conn = new MySqlConnection(DatabaseInf.Veritabani))
     {
         using (MySqlCommand cmd = new MySqlCommand())
         {
             using (MySqlBackup mb = new MySqlBackup(cmd))
             {
                 cmd.Connection = conn;
                 conn.Open();
                 mb.ImportFromFile(path);
                 conn.Close();
                 MessageBox.Show("Başarıyla yüklendi.");
             }
         }
     }
 }
Exemplo n.º 14
0
 private void Restore_Button_Click(object sender, EventArgs e)
 {
     using (mysqlConnection = new MySqlConnection(connection))
     {
         using (myCommand = new MySqlCommand())
         {
             using (mybackup = new MySqlBackup(myCommand))
             {
                 myCommand.Connection = mysqlConnection;
                 mysqlConnection.Open();
                 mybackup.ImportFromFile(Restore_Text.Text);
                 MessageBox.Show("Database Restore Successfully");
                 mysqlConnection.Close();
             }
         }
     }
 }
Exemplo n.º 15
0
        void Backup()
        {
            string connstr = "server=localhost;user=root;pwd=1234;sslmode=none;convertdatetime=true;";

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

                        cmd.CommandText = "show databases;";
                        MySqlDataAdapter da       = new MySqlDataAdapter(cmd);
                        DataTable        dtDbList = new DataTable();
                        da.Fill(dtDbList);

                        string defaultFolder = "C:\\backup_folder";


                        string   defaultBackupFolder = "C:\\backup_folder";
                        string[] files = System.IO.Directory.GetFiles(defaultBackupFolder);

                        foreach (string file in files)
                        {
                            if (file.ToLower().EndsWith(".sql"))
                            {
                                string dbName = System.IO.Path.GetFileNameWithoutExtension(file);

                                cmd.CommandText = "create database if not exists `" + dbName + "`";
                                cmd.ExecuteNonQuery();

                                cmd.CommandText = "use `" + dbName + "`";
                                cmd.ExecuteNonQuery();

                                mb.ImportFromFile(file);
                            }
                        }

                        conn.Close();
                    }
                }
            }
        }
Exemplo n.º 16
0
        private void BtnCr_Click(object sender, EventArgs e)
        {
            string connString;
            string file = (RestorePathBox.Text);

            if (string.IsNullOrEmpty(this.ServerTextBox.Text) || string.IsNullOrEmpty(this.ServerPortTextBox.Text) || string.IsNullOrEmpty(this.DatabaseTextBox.Text) || string.IsNullOrEmpty(this.UserIdTextBox.Text))
            {
                MessageBox.Show("Please check fields in details section.");
            }
            else if (string.IsNullOrEmpty(this.RestorNewCharSetBox.Text))
            {
                MessageBox.Show("Please choose the charset for restore");
            }
            else if (string.IsNullOrEmpty(this.RstorDatabaseNameBox.Text))
            {
                MessageBox.Show("Please type new database name to restore");
            }
            else
            {
                connString = "server=" + (ServerTextBox.Text) + ";" + "port=" + (ServerPortTextBox.Text) + ";" + "database=" + (DatabaseTextBox.Text) + ";" + "uid=" + (UserIdTextBox.Text) + ";" + "password="******";" + "charset=" + (RestorNewCharSetBox.Text) + ";" + "convertzerodatetime=true;";
                try
                {
                    using (MySqlConnection conn = new MySqlConnection(connString))
                    {
                        using (MySqlCommand cmd = new MySqlCommand())
                        {
                            using (MySqlBackup mb = new MySqlBackup(cmd))
                            {
                                cmd.Connection = conn;
                                conn.Open();
                                mb.ImportInfo.TargetDatabase         = (RstorDatabaseNameBox.Text);
                                mb.ImportInfo.DatabaseDefaultCharSet = (RestorNewCharSetBox.Text);
                                mb.ImportFromFile(file);
                                conn.Close();
                                MessageBox.Show("Restore Complete!");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Exemplo n.º 17
0
    protected void UploadDB(string sFileName)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

        using (MySqlConnection conn = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand())
            {
                using (MySqlBackup mb = new MySqlBackup(cmd))
                {
                    cmd.Connection = conn;
                    conn.Open();
                    mb.ImportFromFile(Server.MapPath("~/Databases/") + sFileName); // Lee el archivo en el servidor
                    conn.Close();
                }
            }
        }
    }
Exemplo n.º 18
0
        private void button7_Click(object sender, EventArgs e)
        {
            Class1         myinfo = new Class1();
            string         file   = "D:\\MadaTec\\db\\dbbackup" + myinfo.SqlDateFormat(System.DateTime.Now) + ".sql";
            OpenFileDialog ofd    = new OpenFileDialog();

            ofd.ShowDialog();
            file = ofd.FileName;
            MySqlConnection con = new MySqlConnection(myinfo.ConStr);
            MySqlCommand    cmd = new MySqlCommand();

            cmd.Connection = con;
            MySqlBackup bm = new MySqlBackup(cmd);

            con.Open();
            bm.ImportFromFile(file);
            con.Close();
        }
Exemplo n.º 19
0
        private void Restore(string sMySQLDatabase, string sFilePath)
        {
            string constring = "server=" + sMySQLServer + ";user="******";pwd=" + sMySQLPassword + ";database=" + sMySQLDatabase + ";";

            //string file = "C:tempbackup.sql";
            using (MySqlConnection conn = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    MySqlBackup mb = new MySqlBackup(cmd);

                    cmd.Connection = conn;
                    conn.Open();
                    mb.ImportFromFile(sFilePath);
                    conn.Close();
                }
            }
        }
Exemplo n.º 20
0
        private void Restore()
        {
            string file = "C:\\backup.sql";

            using (MySqlConnection conn = new MySqlConnection(constr))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ImportFromFile(file);
                        conn.Close();
                    }
                }
            }
        }
Exemplo n.º 21
0
        }///// End backup Function

        public void Restore()
        {
            string file = "C:\\backup.sql";

            using (MySqlConnection conn = new MySqlConnection(this.ConnectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ImportFromFile(file);
                        conn.Close();
                    }
                }
            }
        } //////// End Restore database function
Exemplo n.º 22
0
        public static void RestoreDatabase(string sPath)
        {
            string sProvider = "server=" + clsVariables.sIPAddress + ";uid=" + clsVariables.sDbUser + ";" + "pwd=" + clsVariables.sDbPassword + ";database=" + clsVariables.sDbName;

            using (MySqlConnection conn = new MySqlConnection(sProvider))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ImportFromFile(sPath);
                        conn.Close();
                    }
                }
            }
        }
Exemplo n.º 23
0
        public static void ResetBD()
        {
            string file = Global.pathProject + "Resources\\mantis_base.sql";

            using (MySqlConnection conn = GetDBConnection())
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ImportFromFile(file);
                        conn.Close();
                    }
                }
            }
        }
Exemplo n.º 24
0
 public void restoredb(string file)
 {
     try
     {
         if (this.OpenConnection() == true)
         {
             MySqlCommand cmd = new MySqlCommand();
             MySqlBackup  mb  = new MySqlBackup(cmd);
             cmd.Connection = con;
             mb.ImportFromFile(file);
             this.CloseConnection();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Exemplo n.º 25
0
        /// <summary>
        /// 还原数据库
        /// </summary>
        /// <param name="path">指定还原文件***.sql的绝对路径</param>
        /// <param name="dbName">还原到指定数据库</param>
        /// <returns></returns>
        public static bool RestoreDb(string path, string dbName)
        {
            bool isSuccess = false;

            try
            {
                MySqlConnection myconn = new MySqlConnection(ConfigExtensions.Configuration["DbConnection:MySqlConnectionString"]);
                if (myconn.State == ConnectionState.Closed)
                {
                    myconn.Open();
                }
                try
                {
                    using (MySqlCommand cmmd = new MySqlCommand())
                    {
                        using (MySqlBackup backCmd = new MySqlBackup(cmmd))
                        {
                            cmmd.Connection     = myconn;
                            cmmd.CommandTimeout = 3600;
                            backCmd.ImportInfo.TargetDatabase         = dbName;//前提条件 当前 myconn 中的用户有建库等系列权限
                            backCmd.ImportInfo.DatabaseDefaultCharSet = "utf8";
                            backCmd.ImportFromFile(path);
                            isSuccess = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    //Console.WriteLine($"BackupDB_备份数据库异常 sql:{cmdText}. {ex.Message}", "MYSQLIMPL");
                }
                finally
                {
                    if (myconn.State == ConnectionState.Open)
                    {
                        myconn.Close();
                        myconn.Dispose();
                    }
                }
            }
            catch (Exception)
            {
            }
            return(isSuccess);
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(richTextBox2.Text))
            {
                errorProvider1.SetError(richTextBox2, "This field must be filled up");
                label17.ForeColor = Color.Red;
                MessageBox.Show("A field must be required!");
                //MessageBox.Show("Must choose a the file to restore", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //s.Speak("Must choose a the file to restore");
            }

            else
            {
                string file = richTextBox2.Text;
                using (MySqlConnection conn = ConString.Connection)
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            try
                            {
                                DialogResult diaRes = MessageBox.Show("Restore Database?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                                if (diaRes == DialogResult.Yes)
                                {
                                    cmd.Connection = conn;

                                    mb.ImportFromFile(file);
                                    conn.Close();

                                    //s.Speak("Restored Successfully!");
                                    MessageBox.Show("Successfully Restored!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                MessageBox.Show("Make sure that the path and filename extension(.sql) are correct.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 27
0
        //***** Restaurer la base de donnée : *****
        private void button12_Click(object sender, EventArgs e)
        {
            string constring = "server=192.168.0.101;user=user;pwd=pass;database=BD_parking;";
            string file      = "D:\\backup.sql";

            using (MySqlConnection conn = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ImportFromFile(file);
                        conn.Close();
                    }
                }
            }
        }
Exemplo n.º 28
0
        private void btnrespaldo_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = @"D:\\Usuarios\\backup\\";
            openFileDialog1.Title            = "Buscar el respaldo";
            openFileDialog1.CheckFileExists  = true;
            openFileDialog1.DefaultExt       = "sql";
            openFileDialog1.Filter           = "Archivo de respaldo (*.sql)|*.sql";
            openFileDialog1.FilterIndex      = 2;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                conex = "server=" + txtservidor.Text + ";port=" + numpuerto.Value + ";user="******";pwd=" + txtcontra.Text + ";";
                using (MySqlConnection conn = new MySqlConnection(conex))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            try
                            {
                                if (checkBox2.Checked == true)
                                {
                                    mb.ImportInfo.TargetDatabase = combobd.Text;
                                }

                                cmd.Connection = conn;
                                conn.Open();
                                mb.ImportInfo.IgnoreSqlError = checkBox3.Checked;
                                mb.ImportFromFile(openFileDialog1.FileName);
                                conn.Close();
                                MessageBox.Show("Restauración completada", "Aviso", MessageBoxButtons.OK);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message.ToString(), "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 29
0
        private void btnRestaurarCopia_Click(object sender, EventArgs e)
        {
            try
            {
                string server       = "127.0.0.1";
                string databaseName = "sercorDB";
                string user         = "******";


                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Archivo(*.sql)|*.sql";

                MySqlConnection conectar = new MySqlConnection("server=" + server + ";" +
                                                               "database=" + databaseName + ";" +
                                                               "Uid=" + user + ";" +
                                                               "pwd=" + password + ";" +
                                                               "SslMode=none;");

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    using (MySqlConnection conn = conectar)
                    {
                        using (MySqlCommand cmd = new MySqlCommand())
                        {
                            using (MySqlBackup mb = new MySqlBackup(cmd))
                            {
                                cmd.Connection = conn;
                                conn.Open();
                                mb.ImportFromFile(ofd.FileName);
                                conn.Close();
                            }
                        }
                    }
                    MessageBox.Show("Restauración exitosa", "Sercor", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Sercor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPassAdmin.Enabled = true;
                btnListo.Enabled     = true;
            }
        }
Exemplo n.º 30
0
        public void Restore()
        {
            string constring = "server=localhost;user=root;database=bookingdb;";
            string file      = "C:\\backup.sql";

            using (MySqlConnection conn = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ImportFromFile(file);
                        conn.Close();
                    }
                }
            }
        }