點擊下載自動備份最新sqlserver 數據庫

    點擊按鈕  

    爲了更好的實現自動化,點擊按鈕可達到手動備份數據庫,有效率,簡單,快捷

    public const string DbName = "需要備份的數據庫名稱";
        private static SqlConnection SqlConnection = new SqlConnection("data source=.;Initial Catalog=master;integrated security=SSPI;");
        private static string SqlBackup = "BACKUP DATABASE PersonnelManagementDB TO DISK = '" + AppDomain.CurrentDomain.BaseDirectory+ "DB\\" + DbName + ".bak'";
        private static string SqlRestore = "Alter database "+ DbName + " Set Offline With rollback immediate RESTORE DATABASE  " + DbName + " FROM DISK = '" + AppDomain.CurrentDomain.BaseDirectory + "DB\\" + DbName + ".bak' Alter database " + DbName + " Set Online With Rollback immediate";
        private SqlCommand SqlCommandBackup = new SqlCommand() { Connection = SqlConnection, CommandType = CommandType.Text, CommandText = SqlBackup };
        private SqlCommand SqlCommandRestore = new SqlCommand() { Connection = SqlConnection, CommandType = CommandType.Text, CommandText = SqlRestore };

        public string  DbBackup()
        {
            SqlConnection.Open();
            try
            {
                SqlCommandBackup.ExecuteNonQuery();  //備份
            }
            catch (Exception e)
            {
                string str = e.Message;
                SqlConnection.Close();
                return str;
            }
            SqlConnection.Close();
            return "";
        }
   public void DbRestore()
        {
            SqlConnection.Open();
            try
            {
                SqlCommandRestore.ExecuteNonQuery(); //還原
            }
            catch (Exception e)
            {
                string str = e.Message;
                SqlConnection.Close();
            }
            SqlConnection.Close();
        }
View Code

 控制器

public void DownloadFile()
        {
            try
            {
                DBSqlserverUtils utils = new DBSqlserverUtils();
                var str = DbBackup();//備份數據庫
                if (string.IsNullOrEmpty(str))
                {
                    string fileName = DBSqlserverUtils.DbName + ".bak";
                    string filePath = Path.Combine(Server.MapPath("~/DB/"), fileName);
                    if (System.IO.File.Exists(filePath))
                    {
                        HttpResponse response = System.Web.HttpContext.Current.Response;
                        response.Clear();
                        response.ClearHeaders();
                        response.ClearContent();
                        response.Buffer = true;
                        response.AddHeader("content-disposition", string.Format("attachment; FileName={0}", fileName));
                        response.Charset = "GB2312";
                        response.ContentEncoding = Encoding.GetEncoding("GB2312");
                        response.ContentType = MimeMapping.GetMimeMapping(fileName);
                        response.WriteFile(filePath);
                        response.Flush();
                        response.Close();
                    }
                

                }
            }
            catch (Exception ex)
            {
                CLogServiceUtils.RunLogAdd("DownloadFile數據庫備份異常:" + ex.Message);  //根據自己可添加日誌記錄具體是哪裏的錯誤
            }
        }
View Code

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章