sqlserver备份

/// <summary>
/// sqlserver备份
/// </summary>
public class SqlserverBack : IBack
{
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;
} } }
上一篇:计算机基础part1


下一篇:《MySQL技术内幕》读书笔记