Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# backup sql

public class BackupService
{
    private readonly string _connectionString;
    private readonly string _backupFolderFullPath;
    private readonly string[] _systemDatabaseNames = { "master", "tempdb", "model", "msdb" };

    public BackupService(string connectionString, string backupFolderFullPath)
    {
        _connectionString = connectionString;
        _backupFolderFullPath = backupFolderFullPath;
    }

    public void BackupAllUserDatabases()
    {
        foreach (string databaseName in GetAllUserDatabases())
        {
            BackupDatabase(databaseName);
        }
    }

    public void BackupDatabase(string databaseName)
    {
        string filePath = BuildBackupPathWithFilename(databaseName);

        using (var connection = new SqlConnection(_connectionString))
        {
            var query = String.Format("BACKUP DATABASE [{0}] TO DISK='{1}'", databaseName, filePath);

            using (var command = new SqlCommand(query, connection))
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
        }
    }

    private IEnumerable<string> GetAllUserDatabases()
    {
        var databases = new List<String>();

        DataTable databasesTable;

        using (var connection = new SqlConnection(_connectionString))
        {
            connection.Open();

            databasesTable = connection.GetSchema("Databases");

            connection.Close();
        }

        foreach (DataRow row in databasesTable.Rows)
        {
            string databaseName = row["database_name"].ToString();

            if (_systemDatabaseNames.Contains(databaseName))
                continue;

            databases.Add(databaseName);
        }

        return databases;
    }

    private string BuildBackupPathWithFilename(string databaseName)
    {
        string filename = string.Format("{0}-{1}.bak", databaseName, DateTime.Now.ToString("yyyy-MM-dd"));

        return Path.Combine(_backupFolderFullPath, filename);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: all Substring of String 
Csharp :: the same schemaid is already used for type swagger 
Csharp :: draw on picturebox c# 
Csharp :: get last index C# 
Csharp :: parse strings into words C# 
Csharp :: Data at the root level is invalid. Line 1, position 1. 
Csharp :: web page search c# 
Csharp :: unity render to texture2d 
Csharp :: how to pass id from view to controller in asp.net core 
Csharp :: selenium scroll to element c# 
Csharp :: random string generator c# 
Csharp :: wpf how to focus on element 
Csharp :: pause unity game 
Csharp :: destroy gameobject with tag unity 
Csharp :: append an array in c# 
Csharp :: why is called c# 
Csharp :: set file to read only C# 
Csharp :: npm add auth token 
Csharp :: c# write iformfile 
Csharp :: SieveOfEratosthenes 
Csharp :: serilog .net 6 
Csharp :: select distinct two columns entity framework c# 
Csharp :: preprocessors 
Csharp :: how to access asp button of gridview 
Csharp :: C# System.nanoTime 
Csharp :: asp net saber ip address of client machine IIS 
Csharp :: update browserslist 
Csharp :: how to customize xunit input 
Csharp :: how to detect ajax request in asp.net core 
Csharp :: Merge two List using Linq 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =