none
Backup SQL DataBase RRS feed

  • Question

  • i have database in SQL Server 2012 and its bound to a form from Visual Studio with DataSet,

    So how can i back up whole database from dataset with 

    BACKUP DATABASE { database_name | @database_name_var }   TO <backup_device> [ ,...n ]    [ <MIRROR TO clause> ] [ next-mirror-to ]   [ WITH { DIFFERENTIAL | <general_WITH_options> [ ,...n ] } ] [;]

    and i want backup from Clicking a button on form and savefiledialog for saving the backup

    Thanks

    Saturday, January 23, 2016 8:26 PM

Answers

  • Hi aBsEkh,

    >>So how can i back up whole database from dataset

    From my experience, i never backup the whole database from dataset, could you please share your solution if you solve it. And I create a demo about backup database as below for your reference.

    var connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Demo;Integrated Security=True"; 
    
    var backupFolder = @"d:/bak/"; 
    
    var sqlConStrBuilder = new SqlConnectionStringBuilder(connectionString);
    
                // set backupfilename (you will get something like: "d:/bak/Demo-2016-1-25.bak")
    
    var backupFileName = String.Format("{0}{1}-{2}.bak",
    
                    backupFolder, sqlConStrBuilder.InitialCatalog,
    
                    DateTime.Now.ToString("yyyy-MM-dd"));
    
    using (var connection = new SqlConnection(sqlConStrBuilder.ConnectionString))
    
    {
    
        var query = String.Format("BACKUP DATABASE {0} TO DISK='{1}'",
    
                        sqlConStrBuilder.InitialCatalog, backupFileName);
    
        using (var command = new SqlCommand(query, connection))
    
        {
    
              connection.Open();
    
              command.ExecuteNonQuery();
    
        }
    
    }

    Best regards,

    Cole Wu



    Monday, January 25, 2016 4:47 AM
    Moderator