zoukankan      html  css  js  c++  java
  • mvc项目中实现备份数据库(sqlserver2005)

    功能要求:mvc项目,实现数据库备份(bak文件)

    实现步骤:<a id="backupDB" href="javascript:" >数据库备份</a>

    方法:1、新建类DatabaseMaintenance

        /// <summary>  
        /// 数据库维护  
        /// </summary>  
        public class DatabaseMaintenance  
        {  
            /// <summary>  
            /// 备份数据库  
            /// </summary>  
            /// <param name="fileName">备份文件的路径</param>  
            public static void Backup(string fileName)  
            {  
                //TODO SQL Server only now  
                string sqlConnectionString = ConfigurationManager.ConnectionStrings["HelpStoreContext"].ToString();  
                using (SqlConnection conn = new SqlConnection(sqlConnectionString))  
                {  
                    string dbName = new SqlConnectionStringBuilder(sqlConnectionString).InitialCatalog;  
      
                    string commandText = string.Format(  
                        "BACKUP DATABASE [{0}] TO DISK = '{1}' WITH FORMAT",  
                        dbName,  
                        fileName);  
      
                    DbCommand dbCommand = new SqlCommand(commandText, conn);  
                    if (conn.State != ConnectionState.Open)  
                        conn.Open();  
                    dbCommand.ExecuteNonQuery();  
                }  
            }  
     
            /// <summary>  
            /// 还原数据库 database  
            /// </summary>  
            /// <param name="fileName">要还原的数据库文件路径</param>  
            public static void RestoreBackup(string fileName)  
            {  
                string sqlConnectionString = ConfigurationManager.AppSettings["HelpStoreContext"];  
                using (SqlConnection conn = new SqlConnection(sqlConnectionString))  
                {  
                    string dbName = new SqlConnectionStringBuilder(sqlConnectionString).InitialCatalog;  
                    string commandText = string.Format(  
                        "DECLARE @ErrorMessage NVARCHAR(4000)
    " +  
                        "ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    " +  
                        "BEGIN TRY
    " +  
                            "RESTORE DATABASE [{0}] FROM DISK = '{1}' WITH REPLACE
    " +  
                        "END TRY
    " +  
                        "BEGIN CATCH
    " +  
                            "SET @ErrorMessage = ERROR_MESSAGE()
    " +  
                        "END CATCH
    " +  
                        "ALTER DATABASE [{0}] SET MULTI_USER WITH ROLLBACK IMMEDIATE
    " +  
                        "IF (@ErrorMessage is not NULL)
    " +  
                        "BEGIN
    " +  
                            "RAISERROR (@ErrorMessage, 16, 1)
    " +  
                        "END",  
                        dbName,  
                        fileName);  
      
                    DbCommand dbCommand = new SqlCommand(commandText, conn);  
                    if (conn.State != ConnectionState.Open)  
                        conn.Open();  
                    dbCommand.ExecuteNonQuery();  
                }  
      
                //clear all pools  
                SqlConnection.ClearAllPools();  
            }  
      } 

    2、在控制器里调用方法

           //备份数据库  
           public string BackupData()  
           {  
               try  
               {  
                   var dname = DateTime.Now.Ticks;  
                   string filename = Server.MapPath("~/Data/" + dname + ".bak");  
                   if (!System.IO.File.Exists(filename))  
                   {  
                       System.IO.File.Create(filename);  
                   }  
                   DatabaseMaintenance.Backup(filename);  
                   return "备份成功";  
               }  
               catch  
               {  
                   return "备份失败";  
               }  
           }  

    3、删除已经备份的数据库bak文件

          //删除数据库备份文件  
           public string DelDataBase(string id)  
           {  
               try  
               {  
                   string filepath=Server.MapPath("~/Data/"+id);  
                   System.IO.File.Delete(filepath);  
                   return "删除成功";  
               }  
               catch {  
                   return "删除失败";  
               }  
           }  
    
  • 相关阅读:
    这些简单优化能让你的Win10流畅很多
    win7系统登录界面背景怎么修改?
    如何在win7下通过easyBCD引导安装Ubuntu14.04
    为什么我的电脑打不开便签?
    打开Word为什么会出现感叹号呢???
    图像变换原理
    运行
    php、前端开发(网站建设)环境搭建
    zend studio面板功能
    zend studio汉化
  • 原文地址:https://www.cnblogs.com/sky-net/p/4609947.html
Copyright © 2011-2022 走看看