Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to take back up of my windows application database by writing a program.My problem is i can save the .bak file.If we create a backup file to a folder location then we can't choose the same folder path.Its really appreciable if can create a folder with a unique id in each case of creating back up process.


My LOC is
C#
private void button3_Click(object sender, EventArgs e)
{
    try
    {
        System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(ConnectionStringTwo);



        string serverName = builder.DataSource;
        string DbName = builder.InitialCatalog;
        string dbuserName = builder.UserID;
        string dbPassword = builder.Password;

        if (DestPath == "" || DbName == "")
        {
            MessageBox.Show("Try to select Database and Destination Folder !");
        }
        else
        {

            string databaseName = DbName;//dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();

            //Define a Backup object variable.
            Backup sqlBackup = new Backup();

            ////Specify the type of backup, the description, the name, and the database to be backed up.
            sqlBackup.Action = BackupActionType.Database;
            sqlBackup.BackupSetDescription = "BackUp of:" + databaseName + "on" + DateTime.Now.ToShortDateString();
            sqlBackup.BackupSetName = "FullBackUp";
            sqlBackup.Database = databaseName;

            ////Declare a BackupDeviceItem
            string destinationPath = DestPath;
            string backupfileName = DbName+".bak";
            //string backupfileName = DbName+DateTime.Now.Day + DateTime.Now.TimeOfDay + ".bak";
            BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath + "\\" + backupfileName, DeviceType.File);
            ////Define Server connection

            //ServerConnection connection = new ServerConnection(frm.serverName, frm.userName, frm.password);
            ServerConnection connection = new ServerConnection(serverName, dbuserName, dbPassword);
            ////To Avoid TimeOut Exception
            Server sqlServer = new Server(connection);
            sqlServer.ConnectionContext.StatementTimeout = 60 * 60;
            Database db = sqlServer.Databases[databaseName];

            sqlBackup.Initialize = true;
            sqlBackup.Checksum = true;
            sqlBackup.ContinueAfterError = true;

            ////Add the device to the Backup object.
            sqlBackup.Devices.Add(deviceItem);
            ////Set the Incremental property to False to specify that this is a full database backup.
            sqlBackup.Incremental = false;

            sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
            ////Specify that the log must be truncated after the backup is complete.
            sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

            sqlBackup.FormatMedia = false;
            ////Run SqlBackup to perform the full database backup on the instance of SQL Server.
            sqlBackup.SqlBackup(sqlServer);
            ////Remove the backup device from the Backup object.
            sqlBackup.Devices.Remove(deviceItem);
            toolStripStatusLabel1.Text = "Successful backup is created!";
        }
    }
    catch (Exception ex)
    {
        toolStripStatusLabel1.Text = ex.Message;
        // MessageBox.Show(ex.Message);
    }
}


What I have tried:

while creating the back up to the same location
sqlBackup.SqlBackup(sqlServer);gives me an exception of "Back Up failed for server 'Outlet-PC'"
Posted
Updated 10-May-18 3:20am
v3
Comments
Wendelius 10-May-18 8:51am    
Can you post the exact exception message along with inner exceptions (if any)
Bestin P S 10-May-18 9:42am    
An exception occured while executing a Transact-SQl statement or Batch this is what i got
Wendelius 10-May-18 9:55am    
Does the exception contain an inner exception?

1 solution

I know you have written code, but ... would this help?
Backing up an SQL Database in C#[^]
 
Share this answer
 
Comments
Bestin P S 10-May-18 9:39am    
yeah.i can create back up on this LOC but, you know when we trying save the same folder location it got exception.
OriginalGriff 10-May-18 9:49am    
What exception?

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