zoukankan      html  css  js  c++  java
  • 点击下载自动备份最新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
  • 相关阅读:
    记一次git fatal: Unable to find remote helper for 'https'问题的解决
    LambdaMART简介——基于Ranklib源码(二 Regression Tree训练)
    LambdaMART简介——基于Ranklib源码(一 lambda计算)
    用中文把玩Google开源的Deep-Learning项目word2vec
    Ubuntu18.04 一次性升级Python所有库
    CSAPP家庭作业(第二章)
    两个有序链表序列的合并
    sublime text 3 配置Python开发环境
    Java课程设计-泡泡堂(个人)
    二叉树的先序建立与遍历
  • 原文地址:https://www.cnblogs.com/BabyRui/p/14271278.html
Copyright © 2011-2022 走看看