Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a code to take back up of databse in c#.net 2.0
I am using the following code:
Backup sqlBackup = new Backup();
sqlBackup.Action = BackupActionType.Database;
sqlBackup.BackupSetDescription = "ArchiveDataBase:" +
                                             DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = "Archive";
sqlBackup.Database = databaseName;
BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
sqlBackup.Initialize = true;
sqlBackup.Checksum = true;
sqlBackup.ContinueAfterError = true;
sqlBackup.Devices.Add(deviceItem);
sqlBackup.Incremental = false;
sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;
sqlBackup.FormatMedia = false;
sqlBackup.SqlBackup(sqlServer);

At the last line of code I am getting Error like: 'Backup failed for Server 'SERVER\SQLEXPRESS'.'
I am not getting what is the problem. Please help me.
I am not using the Sa login for the database.
Posted
Updated 6-Jan-11 19:07pm
v2
Comments
JF2015 7-Jan-11 1:07am    
Edited to add code formatting. Always wrap your code in <pre> tags.

1 solution

Here's some c# code which will back up your user databases - modify it to your needs:
C#
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace DBBackupCS
{
    class Program
    {
        static void Main(string[] args)
        {
            Server srv = new Server("MyServer\MyInstance");
            ServerConnection srvConn = srv.ConnectionContext;
            srvConn.LoginSecure = true;
            string strBackupDir = srv.Settings.BackupDirectory;
            DatabaseCollection objDatabases = srv.Databases;
            foreach (Database objDB in objDatabases)
            {
                if (objDB.IsSystemObject == false && objDB.IsMirroringEnabled == false)
                {
                    DateTime datNow = DateTime.Now;
                    string strDBName = objDB.Name;
                    string strBKDate = datNow.ToString("yyyyMMddhhmmss");
                    Backup objBackup = new Backup();
                    objBackup.Action = BackupActionType.Database;
                    objBackup.Database = strDBName;
                    objBackup.BackupSetDescription = "Full backup of " + strDBName;
                    objBackup.BackupSetName = strDBName + " Backup";
                    objBackup.MediaDescription = "Disk";
                    objBackup.Devices.AddDevice(strBackupDir + "\\" + strDBName + "_db_" + strBKDate + ".bak", DeviceType.File);
                    objBackup.SqlBackup(srv);
                    if (objDB.DatabaseOptions.RecoveryModel != RecoveryModel.Simple)
                    {
                        datNow = DateTime.Now;
                        string strTBKDate = datNow.ToString("yyyyMMddhhmmss");
                        Backup objTrnBackup = new Backup();
                        objTrnBackup.Action = BackupActionType.Log;
                        objTrnBackup.BackupSetDescription = "Trans Log backup of " + strDBName;
                        objTrnBackup.BackupSetName = strDBName + " Backup";
                        objTrnBackup.Database = strDBName;
                        objTrnBackup.MediaDescription = "Disk";
                        objTrnBackup.Devices.AddDevice(strBackupDir + "\\" + strDBName + "_tlog_" + strTBKDate + ".trn", DeviceType.File);
                        objTrnBackup.SqlBackup(srv);
                   }
                }
            }
        }
    }
}

Remember to incude this at the top of your code:
using Microsoft.SqlServer.Management.Smo;<br />
using Microsoft.SqlServer.Management.Common;


and include references to :
MSIL
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SqlEnum
 
Share this answer
 
v2
Comments
JF2015 7-Jan-11 1:15am    
Fixed code formatting.
Yatin Bhagat 7-Jan-11 5:13am    
This code give Connection error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900