private void btnBackup_Click(object sender, EventArgs e) { //Check pw if (txtPass.Text != "88") { MessageBox.Show("Pass sai"); return; } string backUpFileName = this.fileNameWithoutExt + ".bak"; string fullBackupPath = this.folderPath + backUpFileName; string targetFile = folderPath + fileNameWithoutExt + ".Klib"; //Open Klib file---------------------------------------------- FileStream fs = File.OpenRead(targetFile); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); //Decrypt------------------------------------------------------------------------------------- byte[] buffer1 = SuperKnuth.KnuthTools.DeHash(buffer); //Save file---------------------------------------------------------------- File.WriteAllBytes(fullBackupPath, buffer1); string sqlBackUp = string.Format(@"RESTORE DATABASE {0} FROM DISK = N'{1}' WITH FILE=1, MOVE 'library_data_Data' TO '{2}\{0}.mdf', MOVE 'library_data_Log' TO '{2}\{0}.ldf', NOUNLOAD, STATS = 5;" , txtDB.Text, fullBackupPath, folderPath); SqlConnection cn = new SqlConnection(DataProvider.GetSqlConnectionString()); SqlCommand cmdBkUp = new SqlCommand(sqlBackUp, cn); try { cn.Open(); cmdBkUp.ExecuteNonQuery(); MessageBox.Show("Done"); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { File.Delete(fullBackupPath); if (cn.State == ConnectionState.Open) { cn.Close(); } } }
public void BackupDatabase() { string backUpFileName = this.fileNameWithoutExt + ".bak"; string fullBackupPath = this.folderPath + backUpFileName; string SQLBackUp = string.Format(@"BACKUP DATABASE {0} TO DISK = N'{1}'", dbName, fullBackupPath); SqlConnection cn = new SqlConnection(DataProvider.GetSqlConnectionString()); SqlCommand cmdBkUp = new SqlCommand(SQLBackUp, cn); try { cn.Open(); cmdBkUp.ExecuteNonQuery(); FileStream fs = File.OpenRead(fullBackupPath); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); //Encrypt byte[] buffer1 = SuperKnuth.KnuthTools.Hash(buffer); //Save file File.WriteAllBytes(fullBackupPath.Replace(".bak", ".Klib"), buffer1); File.Delete(fullBackupPath); MessageBox.Show("Done"); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (cn.State == ConnectionState.Open) { cn.Close(); } } }
private void btnDelete_Click(object sender, EventArgs e) { //Check pw if (txtPass.Text != "88") { MessageBox.Show("Pass sai"); return; } string sqlAlter = string.Format("alter database {0} set single_user with rollback immediate", txtDB.Text); string sqlDelete = string.Format("DROP DATABASE {0}", txtDB.Text); string cnString = DataProvider.GetSqlConnectionString(); cnString = cnString.Replace(txtDB.Text, "master"); using (SqlConnection cn = new SqlConnection(cnString)) { SqlCommand cmdBkUp = new SqlCommand(sqlAlter, cn); try { cn.Open(); cmdBkUp.ExecuteNonQuery(); cmdBkUp = new SqlCommand(sqlDelete, cn); cmdBkUp.ExecuteNonQuery(); MessageBox.Show("Xóa thành công"); this.Close(); } catch (Exception ex) { MessageBox.Show("Xóa thất bại\r\n" + ex.Message); } } }