SQL Server DB Backup Using Stored Procedure In WinForm Application

In this article, we are going to learn the process of getting database backup using stored procedure in SQL Server using a C# WinForm application.
 
Let's see the steps to create the database backup file.
 
Step 1
 
First, we will create a database in SQL Server. Here, I have created the database and named it as "SampleDBTest".
 
Please find the below image for your reference.
 
 
 
Step 2
 
Let's create a stored procedure to take a back up on our local disk. To create a stored procedure, please refer to  the image.
 
 
 
Step 3
 
Let's write the query in the stored procedure to make a database backup file on the disk.
 
 
Please use the query if you need it.
  1. create proc SqlDBBackup  
  2. as  
  3. begin  
  4. declare @path varchar(1000);  
  5. set @path='D:\Backup\SampleDBTest3'+CONVERT(CHAR(10),  GETDATE(), 121)+'.bak';  
  6. BACKUP DATABASE SampleDBTest to DISK=@path;  
  7. end  
Once the procedure has been created, you can see your created stored procedure under "programmability" tab.
 
Step 4
 
Let's create a simple C# Windows application in Visual Studio.
 
To create a new Windows application in Visual Studio, go to Visual Studio -> New Project -> select "Windows" tab in the left pane (Note : choose the language C# or VB) -> name your project and click OK.
 
Now, you can see an empty Form window. Let's drag one button from the toolbox and change the label text to "Get DB Backup".
 
Please find the sample image here.
 
 
Step 5
 
Let's add our database to our project using "Add Connection" method.
 
 
Step 6
 
Let's create a connection string for our database in App.Config.
 
 
Step 7
 
Now, we will write the C# code for calling our stored procedure to take the database backup.

 
 
Please find the code  below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. using System.Configuration;  
  11.   
  12. namespace TextilePro  
  13. {  
  14.     public partial class frmWorkingForm : Form  
  15.     {  
  16.         public frmWorkingForm()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TextileProConString"].ToString());  
  21.         private void button1_Click(object sender, EventArgs e)  
  22.         {  
  23.             con.Open();  
  24.             SqlCommand cmd = new SqlCommand("SqlDBBackup", con);  
  25.             cmd.CommandType = CommandType.StoredProcedure;  
  26.             cmd.ExecuteNonQuery();  
  27.             con.Close();  
  28.             MessageBox.Show("Back up Done successfully","SQLDB Backup",MessageBoxButtons.OK,MessageBoxIcon.Information);  
  29.         }  
  30.     }  
  31. }  
Step 8

Let's run our project. To run it,  press ctrl+F5. Click "Get DB Backup button" and you will receive a success message, as shown in the image below.
 
 
Let's open our folder and check whether the database backup is created or not. Now, you can see your database backup file in the given directory.
 


Similar Articles