Thursday, 29 March 2012

How to create a database backup using visual studio

I am going to create a backup using a C# Windows forms project. The project is simple. You will have a button to create a database backup in the windows form. Every time that you click the button, a new backup is created.

This project includes the following components:
  • A database backup:
  • The Windows form project:
  • A script with the backup stored procedure: createBackup.sql


The requirements to build this are:
  • Visual Studio 2008 or later
  • SQL Server 2005 or later

Getting started

Let’s start. 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).
The following T-SQL will do this:
create procedure [dbo].[backupdb]

BACKUP DATABASE [test] TO  DISK = N'C:\backup\test.bak'
     , NOINIT
     , NAME = N'test copy'
     , SKIP
     , STATS = 10
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. Subsequent backups are all written to the same file. Everything to do this is in the stored procedure named dbo.backupdb.
To execute the stored procedure use the following command:
exec [dbo].[backupdb]
The stored procedure will create a backup inside the test.bak file. 
Now, let’s start with the Visual Studio application. Open the backup.sln in the file. In the Solution Explorer, double click in the app.config item.  The content of the file should be the following:
<?xml version="1.0" encoding="utf-8" ?>
    <add name="connectionStringName"
         connectionString="Data Source=.\SQLEXPRESS
         ;Initial Catalog=test;Integrated Security=True;async=true "/>
Let me explain this part of the app.config:
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;async=true "/>
This file contains the connection to the SQL Server. In this case, it is a local SQL Server Express Edition. The initial catalog is the database test and it is using integrated security (Windows authentication).
To use the app.config file it is necessary to add the System.configuration:

The system configuration references needs to be added. To do this, in the Solution Explorer, in references right click the button mouse and select add references.
In the references window, select the System Configuration.

In the design pane, double click the backup button to see the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace backup
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
                string ConnectionString = ConfigurationManager.ConnectionStrings["connectionStringName"].ToString();
                SqlConnection cnn = new SqlConnection(ConnectionString);

                SqlCommand cmd = new SqlCommand("backupdb", cnn);
                cmd.CommandType = CommandType.StoredProcedure;



                MessageBox.Show("Backup completed successfully");

            catch (Exception ex)

Note that the following lines are not created by default. These lines of code need to be added in order to connect to SQL Server and to use the App.config file.
using System.Data.SqlClient;
using System.Configuration;
Let me explain these lines of code:
string ConnectionString = ConfigurationManager.ConnectionStrings["connectionStringName"].ToString();
This line of code will save the connection data created in the app.config file in the connectionString.
The following lines of code are used to call the stored procedure backupdb and execute the stored procedure using Visual Studio.
SqlCommand cmd = new SqlCommand("backupdb", cnn);

cmd.CommandType = CommandType.StoredProcedure;


These lines of code will call the stored procedure created and execute it.
That’s it !. You have a button to create a backup.
In the project, just press F5 in order to start the project. In the Windows form press the backup button. You will create the database backup in the c:\backup\test.bak. If the file was created, you successfully created a Project in Visual Studio to generate SQL Server backups !. 


Wednesday, 7 March 2012

Microsoft .NET Frameworks features details

Microsoft .NET Frameworks....

Features and enhancements that has happened with respect to Microsoft .NET Frameworks


To know the series of events happened with respect to .NET framework.

Microsoft .NET Framework Details

Features: .NET Framework Version - V1.0/1.1: Released in Year 2002/2003
  1. CLR 1.0/1.1
  2. C#.NET was introduced
  3. Upgraded to VB.NET from VB 6
  4. Upgraded to ASP.NET from ASP 3
  5. Upgraded to ADO.NET from ADO
  6. Remoting (previously DCOM) was introduced
  7. Web Services introduced
  8. Visual Studio Versions:  2002/2003
Features: NET Framework Version - V 2.0: Released in Year/2005
  1. CLR 2.0
  2. C#/VB/ASP/ADO.NET 2.0
  3. Web Services Enhancements (WSE)
  5. Visual Studio Versions:  2005
Features: NET Framework Version - V 3.0: Released in Year/2006
  1. CLR 2.0
  2. C#/VB/ASP/ADO.NET 2.0
  3. Windows Presentation Foundation (WPF) introduced
  4. Windows Communication Foundation (WCF) introduced
  5. Windows Workflow Foundation (WF) introduced
  6. Windows CardSpace introduced
  7. Visual Studio Versions:  VS 2005
Features: NET Framework Version - V3.5: Released in Year/2008
  1. CLR 2.0
  2. C#/VB/ASP/ADO.NET 3.5
  3. Language Integrated Query (LINQ)
  4. WCF/WPF/WF 3.5
  5. ASP.NET AJAX is built-in
  6. Visual Studio Versions:  VS 2008
Features: NET Framework Version – V3.5 SP1: Released in Year/2009
  1. Silverlight
  2. Entity Framework
Features: NET Framework Version – V4.0: Released in Year/2010
  1. CLR 4.0
  2. C#/VB/ASP/ADO.NET 4.0
  3. LINQ/EF 4.0
  4. WCF/WPF/WF 4.0
  5. Silverlight 3.0
  6. ASP.NET MVC 2.0
  7. F#.NET introduced
  8. Dynamic Programming
  9. Parallel Programming
  10. Visual Studio Versions: VS 2010
Features: NET Framework Version – V4.5:  Beta is released
  1. Visual Studio Versions: VS 2011 
For further reading on V4.5, visit


Total Pageviews