/// <summary> /// 数据库还原 /// </summary> public void Restoredb() { string pathFile = XML_Tool.xml.DataBase.BackupPath + XML_Tool.xml.DataBase.InitialCatalog + ".bak"; //bak文件路径 string dbName = XML_Tool.xml.DataBase.InitialCatalog; //数据库名称 string connectionString = DB_Tool.GetConnectionString(); //数据库连接字符串 if (!File.Exists(pathFile)) { return; } SqlConnection conn = new SqlConnection(connectionString); string cmdStr = string.Format("use master restore database {0} from disk='{1}' with replace", dbName, pathFile);//添加“use master ”解决问题一 SqlCommand cmd = new SqlCommand(cmdStr, conn); try { conn.Open(); cmd.CommandTimeout = 100;//解决问题二 cmd.ExecuteNonQuery(); } catch (Exception ex) { recodeDBBackRecoverMessage(string.Empty, log_recover_exception + ex.Message, 1, true); } finally { conn.Close(); } }
/// <summary> /// 备份按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void db_backup_button_Click(object sender, EventArgs e) { try { #region copy connectionString = DB_Tool.GetConnectionString(); CopyDatabase(); #endregion #region write log recodeDBBackRecoverMessage(string.Empty, log_backup_success, 0, true); MessageBoxEx.Show(log_backup_success); #endregion } catch (Exception ex) { #region write log recodeDBBackRecoverMessage(string.Empty, log_backup_exception + ex.Message, 1, true); #endregion } }