private void btImport_Click(object sender, EventArgs e) { try { if (_ba == null || _ba.Length == 0) { MessageBox.Show("No content is loaded into memory, cannot perform Import/Restore task."); return; } MemoryStream ms = new MemoryStream(_ba); MySqlConnection conn = new MySqlConnection(Program.ConnectionString); MySqlCommand cmd = new MySqlCommand(); MySqlBackup mb = new MySqlBackup(cmd); cmd.Connection = conn; conn.Open(); mb.ImportFromMemoryStream(ms); conn.Close(); MessageBox.Show("Finished."); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public bool Restore(string path, string password = "", string targetDatabase = "") { try { if (this.Connection != null) { if (this.Connection is SqlConnection) { } else if (this.Connection is MySqlConnection) { using (var zip = ZipFile.Read(path)) { if (!string.IsNullOrEmpty(password)) { zip.Password = password; } zip.Encryption = EncryptionAlgorithm.PkzipWeak; zip.StatusMessageTextWriter = Console.Out; var entry = zip["db.sql"]; if (entry != null) { using (var stream = new MemoryStream()) { entry.Extract(stream); stream.Seek(0, SeekOrigin.Begin); using (MySqlCommand cmd = new MySqlCommand()) { using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = this.Connection; this.Connection.Open(); if (!string.IsNullOrEmpty(targetDatabase)) { mb.ImportInfo.TargetDatabase = targetDatabase; } mb.ImportInfo.DatabaseDefaultCharSet = "utf8"; mb.ImportFromMemoryStream(stream); } } this.Connection.Close(); return(true); } } } } else if (this.Connection is SQLiteConnection) { } } } catch (Exception ex) { this.Connection.Close(); Log.Trace(ex.Message, ex.StackTrace); } return(false); }
protected void btImport(object sender, EventArgs e) { try { if (!FileUpload1.HasFile) { return; } byte[] ba = FileUpload1.FileBytes; if (FileUpload1.FileName.ToLower().EndsWith(".zip")) { MemoryStream ms1 = new MemoryStream(ba); ZipStorer zip = ZipStorer.Open(ms1, FileAccess.Read); List <ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir(); MemoryStream ms2 = new MemoryStream(); zip.ExtractFile(dir[0], ms2); zip.Close(); ba = ms2.ToArray(); } MemoryStream ms3 = new MemoryStream(ba); using (MySqlConnection conn = new MySqlConnection(txtConnString.Text)) { MySqlCommand cmd = new MySqlCommand(); MySqlBackup mb = new MySqlBackup(cmd); cmd.Connection = conn; conn.Open(); mb.ImportFromMemoryStream(ms3); } Header.Controls.Add(new LiteralControl("<script type=\"text/javascript\">alert('Import completed.');</script>")); } catch (Exception ex) { lbError.Text = ex.Message; lbError.Visible = true; } }
public static Task Restore(string file_name, DatabaseConnectionProperties dbconnprop = null) { return(Task.Run(async() => { using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(file_name))) { ZipFile zf = new ZipFile(ms); zf.IsStreamOwner = false; byte[] buffer = new byte[4096]; using (MemoryStream zps = new MemoryStream()) { StreamUtils.Copy(zf.GetInputStream(zf[0]), zps, buffer); string connection_string; if (dbconnprop != null) { connection_string = dbconnprop.ConnectionString; } else { connection_string = MCv2Persistance.Instance.Config.DatabaseConfiguration.DatabaseConnectionProperties.ConnectionString; } using (MySqlConnection sqlconn = new MySqlConnection(connection_string)) using (MySqlCommand cmd = new MySqlCommand()) using (MySqlBackup mb = new MySqlBackup(cmd)) { cmd.Connection = sqlconn; await sqlconn.OpenAsync(); mb.ImportFromMemoryStream(zps); } } } })); }