none
Making a backup of database RRS feed

  • Question

  • I am posting a solution as previously provided by karen Payne for making a backup.  i have tried modifying several items from my meager knowledge.  But gave up when I got my first try at compiling bounced at me.  Among many questions I had was "where is button1.click" referenced in the second line?  Anyway, if Karen is listening or anyone else, if you judge that you can help me thru some communications, I'm all ears.  Otherwise can somone suggest a more "canned" routine that worked previously in an environment similar to what I am in now (Visual Studio 2015 community VB)?

    Imports System.Data.SqlClient
        Public Class Form1
            Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Dim connectionString = My.Settings.ConnectionString
            Dim connectionString = "Data Source ARISE\MSSQLLocalDB;AttachDbFilename="C:\Users\ARISE\documents\visual studio 2015\Projects\NU2012\NU2012\App_Data\First.mdf";Integrated Security=True;Connect Timeout=30"
    
            ' read backup folder from config file ("C:MyBackUps")
            'Dim backupFolder = My.Settings.BackupFolder ' ConfigurationManager.AppSettings["BackupFolder"];
            Dim backupFolder = ConfigurationManager.AppSettings.BackupFolder
    
            Dim Builder = New SqlConnectionStringBuilder(connectionString)
    
                ' set backupfilename (you will get something like: "C:\temp\MyDatabase-2013-12-07.bak")
                Dim backupFileName = String.Format("{0}{1}_KP1.bak", backupFolder, Builder.InitialCatalog)
    
                Console.WriteLine(backupFileName)
    
                Using connection = New SqlConnection(Builder.ConnectionString)
                    Dim query = String.Format("BACKUP DATABASE {0} TO DISK='{1}'", Builder.InitialCatalog, backupFileName)
    
                    Using command = New SqlCommand(query, connection)
                        connection.Open()
                        command.ExecuteNonQuery()
                    End Using
                End Using
            End Sub
        End Class
    'I setup connection And backup folder As follows And note I am Using a full version Of SQL-Server so my data source Is KARENS-PC, Not (local) Or .\SQLEXPRESS. Backup folder must exists.
    
    '<?xml version="1.0" encoding="utf-8" ?>
    '<Configuration>
    '    <configSections>
    '    <sectionGroup name = "userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
    '            <section name = "WindowsFormsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    '        </sectionGroup>
    '    </configSections>
    '    <connectionStrings>
    '    <add name = "WindowsFormsApplication1.Properties.Settings.ConnectionString"
    '            connectionString="Data Source=KARENS-PC;Initial Catalog=CustomerDatabase;Integrated Security=True"
    '            providerName="System.Data.SqlClient" />
    '    </connectionStrings>
    '    <startup>
    '    <supportedRuntime version = "v4.0" sku=".NETFramework,Version=v4.5.2" />
    '    </startup>
    '    <userSettings>
    '    <WindowsFormsApplication1.Properties.Settings>
    '    <Setting name = "BackupFolder" serializeAs="String">
    '                <value> C : \Data\Backup\</value>
    '            </setting>
    '        </WindowsFormsApplication1.Properties.Settings>
    '    </userSettings>
    '</configu
    

     
    Saturday, November 21, 2020 7:30 PM

All replies

  • forum migrated to mentioned below link please use this

    https://docs.microsoft.com/en-us/answers/index.html

     hope answer the question if issue resolve please accept answer

    Saturday, November 21, 2020 7:32 PM
  • Hi, 
    button_Clicked is the event raised by a button in Class.
    Normal this class is based on (windows) form and the button is draged in design-mode from toolbox to the form.
    So maybe you missed to add this button  to your form.


    But advise: the backup should made by a Backup Job in the SQL Server.
    Multiple suggestions to make a SQL Server Backup:
    https://www.sqlshack.com/multiple-methods-for-scheduling-a-sql-server-backup-automatically/
    HTH

    Regards Alexander

    Saturday, November 21, 2020 11:17 PM