

These lines of code need to be added in order to connect to SQL Server and to use the App.config file. Note that the following lines are not created by default. MessageBox.Show("Backup completed successfully") SqlCommand cmd = new SqlCommand("backupdb", cnn) Ĭmd.CommandType = CommandType.StoredProcedure SqlConnection cnn = new SqlConnection(ConnectionString) Private void button1_Click(object sender, EventArgs e) In the design pane, double click the backup button to see the following code: In the references window, select the System Configuration. To do this, in the Solution Explorer, in references right click the button mouse and select add references. The system configuration references needs to be added. To use the app.config file it is necessary to add the nfiguration: The initial catalog is the database test and it is using integrated security (Windows authentication). In this case, it is a local SQL Server Express Edition. This file contains the connection to the SQL Server. Let me explain this part of the app.config:ĬonnectionString="Data Source=.\SQLEXPRESS Initial Catalog=test Integrated Security=True async=true "/> The content of the file should be the following:

Open the backup.sln in the backup.zip file. In the Solution Explorer, double click in the app.config item. Now, let’s start with the Visual Studio application. The stored procedure will create a backup inside the test.bak file.

To execute the stored procedure use the following command: Everything to do this is in the stored procedure named dbo.backupdb. Subsequent backups are all written to the same file. Peace of cake, isn’t it? We are creating the test database backup in the c:\backup folder and the backup name is test.bak. We are going to create a stored procedure first that creates a backup of the test database (you can create a test database or restore from the test.bak attached manually).īACKUP DATABASE TO DISK = N'C:\backup\test.bak' A script with the backup stored procedure: createBackup.sql.This project includes the following components: Every time that you click the button, a new backup is created. You will have a button to create a database backup in the windows form. I am going to create a backup using a C# Windows forms project. The project is simple.
