zoukankan      html  css  js  c++  java
  • C# 实现SQLServer数据库备份示例



    C# 实现SQLServer数据库备份示例:

       /// <summary>

        /// sqlserver备份

        /// </summary>

        public class SqlserverBack 

        {

            private string backPath = string.Empty;


            public SqlserverBack()

            {

                backPath = ConfigurationManager.AppSettings["backPath"];

            }


            public string Back(string connstr)

            {

                if (string.IsNullOrWhiteSpace(connstr))

                {

                    throw new ArgumentNullException("连接字符串不能为空");

                }

                using (SqlConnection conn = new SqlConnection(connstr))

                {

                    conn.Open();


                    string dbName = conn.Database;


                    string backDirectory = Path.Combine(backPath, dbName);

                    if (!Directory.Exists(backDirectory))

                    {

                        Directory.CreateDirectory(backDirectory);

                    }


                    string backFileName = Path.Combine(backDirectory, $"{dbName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.bak");


                    string backSql = $"backup database {dbName} to disk='{backFileName}' WITH INIT;";


                    SqlCommand cmd = new SqlCommand(backSql, conn);

                    cmd.ExecuteNonQuery();

                    return backFileName;

                }


            }




        }


  • 相关阅读:
    07.swoole学习笔记--tcp客户端
    06.swoole学习笔记--异步tcp服务器
    04.swoole学习笔记--webSocket服务器
    bzoj 4516: [Sdoi2016]生成魔咒
    bzoj 3238: [Ahoi2013]差异
    bzoj 4566: [Haoi2016]找相同字符
    bzoj 4199: [Noi2015]品酒大会
    后缀数组之hihocoder 重复旋律1-4
    二分查找
    内置函数--sorted,filter,map
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351609.html
Copyright © 2011-2022 走看看