Backup/Restore SQL database using C#

by Prashant 22. June 2009 00:59

Microsoft’s .NET framework provides the flexibility to Backup and Restore SQL server database. A few lines to populate the names of all the server instances, create backups and then restore the backups. The application what you see here is a part of an application, so when you create backups and restore it will use the same path hardcoded in the code itself. You can change the way you like it.


Namespace you need to add:  

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;

I am not going to write on and on and just make it going boring, so it's better just download the code and check the comments, they are one liners, but clears everything. 

Download Full Application Here: DBBackup.zip (290.99 kb)

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: , , ,

C# | SQL Server | Utils

Comments (15) -

paras patel
paras patel India
9/11/2011 12:35:02 AM #

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;

how can i insert this three line in my c# project and give this namespace for the sql server plz give me help

Reply

prashant
prashant India
9/11/2011 1:04:09 AM #

You need to manually add the reference in your project.

Right-Click References in Solution Explorer and under .NET tab add the above 2 namespaces.

Reply

paras patel
paras patel India
9/11/2011 1:09:29 AM #

thank you sir give me reply bet I cant get it namespace in my visual studio 2005 how can I insert and give specific name of .net name space


thankyou

Reply

prashant
prashant India
9/11/2011 1:24:56 AM #

You need to add Microsoft.SqlServer.ConnectionInfo namespace from the .NET tab in the Add Reference dialog box.

Reply

paras patel
paras patel India
9/11/2011 1:33:52 AM #

this name space is not my .net references  so how can I possible this  

Reply

prashant
prashant India
9/11/2011 2:02:34 AM #

Have you got SQL server installed on your machine?

If you still can't find the namespace, then check the above link in the comments from stackoverflow and download the MSI (installer) for that.

Reply

paras patel
paras patel India
9/11/2011 2:50:00 AM #

I get on file using Microsoft.SqlServer.Management.Common;
but second file I cant get it and not download so plz give me where I will find It
using Microsoft.SqlServer.Management.Smo; this file is not get

Reply

mohamed shareef.p
mohamed shareef.p India
11/4/2011 10:43:11 AM #

thank you...........!!!!!!!

Reply

Nguyễn Duy Nhân
Nguyễn Duy Nhân Vietnam
11/10/2011 12:18:42 PM #

I am doing an assignment on Backup + Restore database with Stored Procedures.
Backup is ok, but Restore the error: “RESTORE cannot process database ‘db_test’ because it is in use by this session. It is recommended that the master database be used when performing this operation.”.
I tried to fix but the document has not been, hope admin help with this problem. Thanks!
Link source: http://www.mediafire.com/?c1uldq2t7eczw3b
I use C # VS2010 + sql server 2005.

Reply

Prashant
Prashant India
11/10/2011 7:10:03 PM #

You are using your own custom Stored Procedures to take the backup....and this is why you are using the same database you to backup and restore. Let's say this: Open a file and then try deleting it. It won't let you do that and prompt with the error message that the file is in use. Here you have to change the database to Master database and then use the in-built T-SQL function to restore or backup the database.

Read gere more about using these msdn.microsoft.com/en-us/library/ms186858.aspx

Reply

robin
robin India
1/2/2012 8:33:15 PM #

It doesnot show the names of all the server instances in windows 7,can you help me

Reply

62071072
62071072 United Kingdom
1/28/2012 2:45:51 AM #

Hi I am doing backup  of selected tables for selected database and selected server .
Now i need to backup stored procedures as well for few databases . Please help how can i do this ?

Reply

Prashant
Prashant India
1/28/2012 7:06:40 AM #

If you taking the backup of the complete database then there is no need to backup the stored procedure separately.  The above code/app will take the complete backup of the DB and not of the DB entities.

Reply

62071072
62071072 United Kingdom
1/31/2012 12:07:29 AM #

I am not doing the complete database backup  .  Let me say how my interface looks ..
Form contains servername where user needs to select the server when user selects server then list of dtabases will be populated for corresponding server and when user selects database then list of tables will be populated for corresponding database and user selects the tables need to backup and when he does backup the backup is done in textfile for selected tables with options tablestructure,data and table structure or only data .
Now i want to provide another option if user selects storedprocedure then it should display all the stroed procedures for corresponding database and when select backup it should do backup in a textfile of all the stored procedures.

Reply

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Visit blogadda.com to discover Indian blogs Computers Blogs

About Me

Name of authorPrashant Khandelwal.
Programmer and tech enthusiast. More...

Feeds Subscribe Twitter Facebook Google Plus Linked In Delicious

My Visual Studio Achievements

Badges

Month List

Blog Stats

321,872 Hits

Adverts

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012

Creative Commons License