数据库恢复、备份、分离、附加类

来源:互联网 发布:java 小游戏 编辑:程序博客网 时间:2024/06/11 22:05
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Data.SqlClient; using System.Data; using System.ServiceProcess; namespace AdminZJC.DataBaseControl {  ///   /// 数据库操作控制类  ///   public class DataBaseControl  {  /// /// 数据库连接字符串///public string ConnectionString;/// /// SQL操作语句/存储过程///public string StrSQL;/// /// 实例化一个数据库连接对象///private SqlConnection Conn;/// /// 实例化一个新的数据库操作对象Comm///private SqlCommand Comm;/// /// 要操作的数据库名称///public string DataBaseName;/// /// 数据库文件完整地址///public string DataBase_MDF;/// /// 数据库日志文件完整地址///public string DataBase_LDF;/// /// 备份文件名///public string DataBaseOfBackupName;/// /// 备份文件路径///public string DataBaseOfBackupPath;/// /// 执行创建/修改数据库和表的操作///public void DataBaseAndTableControl(){try{Conn = new SqlConnection(ConnectionString);Conn.Open();Comm = new SqlCommand();Comm.Connection = Conn;Comm.CommandText = StrSQL;Comm.CommandType = CommandType.Text;Comm.ExecuteNonQuery();MessageBox.Show("数据库操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception ex){MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}finally{Conn.Close();}}/// /// 附加数据库///public void AddDataBase(){try{Conn = new SqlConnection(ConnectionString);Conn.Open();Comm = new SqlCommand();Comm.Connection = Conn;Comm.CommandText = "sp_attach_db";Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));Comm.Parameters[@"dbname"].Value = DataBaseName;Comm.Parameters.Add(new SqlParameter(@"filename1", SqlDbType.NVarChar));Comm.Parameters[@"filename1"].Value = DataBase_MDF;Comm.Parameters.Add(new SqlParameter(@"filename2", SqlDbType.NVarChar));Comm.Parameters[@"filename2"].Value = DataBase_LDF;Comm.CommandType = CommandType.StoredProcedure;Comm.ExecuteNonQuery();MessageBox.Show("附加数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception ex){MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}finally{Conn.Close();}}/// /// 分离数据库///public void DeleteDataBase(){try{Conn = new SqlConnection(ConnectionString);Conn.Open();Comm = new SqlCommand();Comm.Connection = Conn;Comm.CommandText = @"sp_detach_db";Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));Comm.Parameters[@"dbname"].Value = DataBaseName;Comm.CommandType = CommandType.StoredProcedure;Comm.ExecuteNonQuery();MessageBox.Show("分离数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception ex){MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}finally{Conn.Close();}}/// /// 备份数据库///public void BackupDataBase(){try{Conn = new SqlConnection(ConnectionString);Conn.Open();Comm = new SqlCommand();Comm.Connection = Conn;Comm.CommandText = "use master;backup database @dbname to disk = @backupname;";Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));Comm.Parameters[@"dbname"].Value = DataBaseName;Comm.Parameters.Add(new SqlParameter(@"backupname", SqlDbType.NVarChar));Comm.Parameters[@"backupname"].Value = @DataBaseOfBackupPath + @DataBaseOfBackupName;Comm.CommandType = CommandType.Text;Comm.ExecuteNonQuery();MessageBox.Show("备份数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception ex){MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}finally{Conn.Close();}}/// /// 还原数据库///public void ReplaceDataBase(){try{string BackupFile = @DataBaseOfBackupPath + @DataBaseOfBackupName;Conn = new SqlConnection(ConnectionString);Conn.Open();Comm = new SqlCommand();Comm.Connection = Conn;Comm.CommandText = "use master;restore database @DataBaseName From disk = @BackupFile with replace;";Comm.Parameters.Add(new SqlParameter(@"DataBaseName", SqlDbType.NVarChar));Comm.Parameters[@"DataBaseName"].Value = DataBaseName;Comm.Parameters.Add(new SqlParameter(@"BackupFile", SqlDbType.NVarChar));Comm.Parameters[@"BackupFile"].Value = BackupFile;Comm.CommandType = CommandType.Text;Comm.ExecuteNonQuery();MessageBox.Show("还原数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception ex){MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}finally{Conn.Close();}}}}
原创粉丝点击