You are on page 1of 6

12/28/13

SQL Server Database BackUp using C# | Dot Net World

Dot Net World


C# | ASP.Net | VB.Net | SQL Server | jQuery | Java Script | LINQ | XML | Web Application
Home ASP.Net About Me SQL Server Index C# Contact Me Gridview jQuery Java Script XML LINQ Blogging

Y ou are here Home SQL Server SQL Server Database BackUp using C#
FRIDAY , MARCH 8, 2013 SEARCH

SQL Server Database BackUp using C#


There are various ways to take the SQL Server database backup. Y ou can take the database backup using SQL Server backup wizard or using SQL Server BackUp Database statement. Here I am going to describe how to take the SQL Server database backup programatically using C# and SQL Server Management Objects (SMO). In my previous posts, I explained Partial Methods,Contextual Keyword, C# Static Methods and some other articles related to C#, ASP.Net and SQL Server . SQL Server Management Objects (SMO) is a collection of objects that are designed for programming all Like aspects of managing Microsoft SQL Server.
107 people like Dot Net World.

Sea r ch

SPONSORS

Dot Net World

For taking the database backup using C#, you have to add the following references in your applicationMicrosoft.SqlServer.ConnectionInfo Microsoft.SqlServer.Management.Sdk.Sfc Microsoft.SqlServer.Smo Microsoft.SqlServer.SmoExtended Microsoft.SqlServer.SqlEnum In your .CS file you will have to use the following namespacesu s i n gM i c r o s o f t . S q l S e r v e r . M a n a g e m e n t . S m o ; u s i n gM i c r o s o f t . S q l S e r v e r . M a n a g e m e n t . C o m m o n ;

Facebook social plugin

After using above namespaces, write the following code to take the database backupp u b l i cv o i dB a c k u p D a t a b a s e ( s t r i n gd a t a b a s e N a m e ,s t r i n gu s e r N a m e ,s t r i n gp a s s w o r d ,s t r i n g s e r v e r N a m e ,s t r i n gd e s t i n a t i o n P a t h ) { / / D e f i n eaB a c k u po b j e c tv a r i a b l e . B a c k u ps q l B a c k u p=n e wB a c k u p ( ) ; / / S p e c i f yt h et y p eo fb a c k u p ,t h ed e s c r i p t i o n ,t h en a m e ,a n dt h ed a t a b a s et ob eb a c k e d u p . s q l B a c k u p . A c t i o n=B a c k u p A c t i o n T y p e . D a t a b a s e ; s q l B a c k u p . B a c k u p S e t D e s c r i p t i o n=" B a c k U po f : "+d a t a b a s e N a m e+" o n "+ D a t e T i m e . N o w . T o S h o r t D a t e S t r i n g ( ) ; s q l B a c k u p . B a c k u p S e t N a m e=" F u l l B a c k U p " ; s q l B a c k u p . D a t a b a s e=d a t a b a s e N a m e ; / / D e c l a r eaB a c k u p D e v i c e I t e m
FOLLOW BY EMAIL

Email address...

Submit

^ Scroll to Top

SUBSCRIBE

manish4dotnet.blogspot.in/2013/03/DatabaseBackupusingCSharp.html

1/6

12/28/13

SQL Server Database BackUp using C# | Dot Net World


Posts Comments

B a c k u p D e v i c e I t e md e v i c e I t e m=n e wB a c k u p D e v i c e I t e m ( d e s t i n a t i o n P a t h+" F u l l B a c k U p . b a k " , D e v i c e T y p e . F i l e ) ; / / D e f i n eS e r v e rc o n n e c t i o n S e r v e r C o n n e c t i o nc o n n e c t i o n=n e wS e r v e r C o n n e c t i o n ( s e r v e r N a m e ,u s e r N a m e ,p a s s w o r d ) ; / / T oA v o i dT i m e O u tE x c e p t i o n S e r v e rs q l S e r v e r=n e wS e r v e r ( c o n n e c t i o n ) ; s q l S e r v e r . C o n n e c t i o n C o n t e x t . S t a t e m e n t T i m e o u t=6 0*6 0 ; D a t a b a s ed b=s q l S e r v e r . D a t a b a s e s [ d a t a b a s e N a m e ] ; s q l B a c k u p . I n i t i a l i z e=t r u e ; s q l B a c k u p . C h e c k s u m=t r u e ; s q l B a c k u p . C o n t i n u e A f t e r E r r o r=t r u e ; / / A d dt h ed e v i c et ot h eB a c k u po b j e c t . s q l B a c k u p . D e v i c e s . A d d ( d e v i c e I t e m ) ; / / S e tt h eI n c r e m e n t a lp r o p e r t yt oF a l s et os p e c i f yt h a tt h i si saf u l ld a t a b a s eb a c k u p . s q l B a c k u p . I n c r e m e n t a l=f a l s e ; s q l B a c k u p . E x p i r a t i o n D a t e=D a t e T i m e . N o w . A d d D a y s ( 3 ) ; / / S p e c i f yt h a tt h el o gm u s tb et r u n c a t e da f t e rt h eb a c k u pi sc o m p l e t e . s q l B a c k u p . L o g T r u n c a t i o n=B a c k u p T r u n c a t e L o g T y p e . T r u n c a t e ; s q l B a c k u p . F o r m a t M e d i a=f a l s e ; / / R u nS q l B a c k u pt op e r f o r mt h ef u l ld a t a b a s eb a c k u po nt h ei n s t a n c eo fS Q LS e r v e r . s q l B a c k u p . S q l B a c k u p ( s q l S e r v e r ) ; / / R e m o v et h eb a c k u pd e v i c ef r o mt h eB a c k u po b j e c t . s q l B a c k u p . D e v i c e s . R e m o v e ( d e v i c e I t e m ) ; }

RECENT POSTS

How T o- Search records or data in gridview using jQuery (16 Jul 2013) (0 comment) How T o- Send DataGridView Data in Email in Window Form Application (23 Jul 2013) (0 comment) SQL Server- User Defined Function to Parse HT MLStrip HT ML -Without Using Regular Expression (06 Aug 2013) (0 comment) LINQ- Difference between Single and SingleOrDefault- Single vs SingleOrDefault BLOG ARCHIVE (12 Aug 2013) comment) (0 2013 (82) Dec 2013 (1) Oct 2013 (1) Aug 2013 (5) Jul 2013 (2) Jun 2013 (4)

Happy coding!!
Posted by Manish Dubey at 5:50 PM 26 Comments Labels: ASP.Net, C#, SQL Serv er
+1 Recommend this on Google

AROUND THE WEB

WHA T ' S T HI S ?

May 2013 (8) Apr 2013 (17) Mar 2013 (18) jQuery- Page Scroll to Top with jQuery | How to ad... Automatically Refresh Page Using Java Script

14 Social Security Benefits You Haven't Been T aking


Moneynews
ALSO ON DOT NET WORLD

9 Ways to Have a Women's Night In


Citi Women & Co.

5 Ways to Stay Young and F it


Stack

How to Create a Textarea Character Counter / Limit... SQL Server STR Function Export Gridview to PDF in ASP.Net

How T o- Bind Data to Gridview using jQuery in ASP.Net ~ Dot Net World : 8 comments How T o- Send DataGridView Data in Email in Window F orm Application ~ Dot 6 comments

How T o- Get hard drive information using C# ~ Dot Net World : Your Source 2 comments How T o- Send Gridview Data in Email in ASP.Net using C#, VB.Net ~ 14 comments

SPACE Function in SQL Server Restore SQL Server Database Using C# SQL Server Database BackUp using C# SQL Server LOWER Function

26 comments Join the discussion


Best Community K aus hal Mehra
7 months ago

C# Anonymous Method SQL Server SUBSTRING Function Animated Sliding Recent Post Widget For Blogger

Share

Login

Change Input to Upper Case using Java Script SQL Server UPPER Function

Used exactly the same code on click of button on my webpage. Getting below error. "Backup failed for server servername" exception thrown at line: sqlBackup.sqlBackup(sqlServer) Calling method like this: protected void Button1_Click(object sender, EventArgs e)

Exporting Query Results to CSV using SQLCMD SQL Server STUFF Function SQL Server LEN Function SQL Server UNICODE Function Feb 2013 (16) Jan 2013 (10)

manish4dotnet.blogspot.in/2013/03/DatabaseBackupusingCSharp.html

2/6

12/28/13
{

SQL Server Database BackUp using C# | Dot Net World


2012 (32)

BackupDatabase("databasenamexxx","usernamexxx","passwordxxx","servernamexxx","C:\\Users\\kaushal\\Downloads"); } Please help.


2
Reply Share

ABOUT ME

Manish Dubey
Follow

123

View my complete profile


Manis h Dubey
Mod

Kaushal Mehra 7 months ago

Can you give me more detail about Error. Please use try-catch block to get more specific error. That will be more helpful for me to help you!!
2
Reply Share

BLOG ROLL

Site Home SQL Authority 2,000 T hings about C#

Nandhinee

Kaushal Mehra a month ago

I too get the same error. Backup failed for Server server name? Can u pls help.
Reply Share

Deenu

4 months ago

how to take database backup using windows authentication mode???


Reply Share

V ik ram

5 months ago

Sir jee,,Could Y ou tell me How to add those 4 namespaces? I am Wondering Every where,,but could not find it yet. Plz Do help
Reply Share

Manis h Dubey

Mod

Vikram 5 months ago

For using those namespaces which are show above. Y ou have to add the references of these namespaces. For thisGo to Y our Application and Right Click on References folder and select Add Reference. Now Go to "Browse" Tab and browse the following path"C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies" Now Select the following dlls Microsoft.SqlServer.ConnectionInfo.dll Microsoft.SqlServer.Management.Sdk.Sfc.dll Microsoft.SqlServer.Smo.dll Microsoft.SqlServer.SmoExtended.dll Microsoft.SqlServer.SqlEnum.dll
Reply Share

CATEGORIES

ASP.Net C#

SQL Server Java Script Code Snippest Jquery Tips and Tricks GridView Linq CSS Database
Blogging Tips DataTable Mail V B.Net Web Serv ice XML ASp.Net Controls Freshers Google API LINQ to SQL PDF Reflection Window Application iTex tSharp A ja x
A ssem blies Blog g er C Colla t ion Con st r u ct or Ex cept ion FileUploa d GMa il Gen er ic Gen er ic Collect ion IL LIst Lin q t o Da t a T a ble Pr im a r y Key Qu er y St r in g Ra d Con t r ol SMT P St r in g Ma n ipu la t ion T iff User Con t r ols V a lida t ion Y ou T u be ZIp File

V ik ram

Manish Dubey 5 months ago

Bhai Done It,, Thank Uuuuuuuuuu A Tight Hug :)


Reply Share

V ik ram

Vikram 5 months ago

Sir It's Working well,,there is another problem as I am giving the database name and path statically,it does not allow me to over write in that same name and same location,In result back up getting failed after first bak up. so i created a text field where user have to give the data base name. but my query is how this is possible to overwrite that same file name and in same location, Do Suggest,Thank u
Reply Share

Ask Manish now

Manis h Dubey

Mod

Vikram 5 months ago

Y ou can not save the same file with same name in one place. If you want to over write the previous file, you should check that file is exists or not on you location if exists then first ti delete that file then save new file. For all these use following links-

manish4dotnet.blogspot.in/2013/03/DatabaseBackupusingCSharp.html

3/6

12/28/13

SQL Server Database BackUp using C# | Dot Net World


http://www.dotnetperls.com/fil... http://www.dotnetperls.com/fil...
Reply Share

V ik ram

Manish Dubey 5 months ago


Reply Share

Thank Y ou Sir jee :)

Chris t ian

6 months ago

everything went good! thank you! i just got an error "Backup failed" then i add the "SQLServer2005MSSQLUser$MY SERVERNAME$MSSQLSERVER" permit on group or users (Security tab) on the destination folder and worked as expected
Reply Share

Raj

Christian 4 months ago


Reply Share

shud i use same for sql server 2008..???

Manis h Dubey

Mod

Raj 4 months ago

Y es use can use it for Server 2008 also


Reply Share

Manis h Dubey

Mod

Christian 6 months ago

Thanks Christian!!
Reply Share

S ameer Das

7 months ago

can it work in online


Reply Share

Manis h Dubey

Mod

Sameer Das 7 months ago

Y es, It will. Have you try it online?


Reply Share

S erver management

8 months ago

Thanks for your posting this blog. Actually i was searchig for server technical solutions on net. Found your blog. After my successful research i found an website IT Monteur who is expert in this field. I hope you also like their service. If you're looking for highly qualified professional server management. If you'd like the services to be affordable but will never sacrifice top quality for the sake of with IT monteur today. If all or most of your business depends on reliable operation of your servers. If youre looking for highly qualified professional server management If youd like the services to be affordable but will never sacrifice top quality for the sake of cheap price. .Our experts will help you select the service package which satisfies your companys needs the best. Whether you need to secure your Linux server or setting it from scratch, manage your network infrastructure or fine tune a database server from small one-time job to a fully outsourced solution IT MONTEUR will meet your needs. Our clients are always satisfied with the final results, and so will you. Contact at +91 120 6498887 or visit at www.itmonteur.net. Industry most useful services:1) Server management 2) Network monitoring 3) Security services 4) Anti Virus & Anti Spam Solutions

IT monteur - a Complete IT Solution provider in Delhi, Noida, Gurgaon.


Reply Share

Manis h Dubey

Mod

Server management 8 months ago

Thanks for you valuable information!!


Reply Share

deepak

9 months ago

manish4dotnet.blogspot.in/2013/03/DatabaseBackupusingCSharp.html

I didn't know this Manish.Wouldn't it be simpler to just run the following syntax from c# instead of

4/6

12/28/13

SQL Server Database BackUp using C# | Dot Net World


I didn't know this Manish.Wouldn't it be simpler to just run the following syntax from c# instead of SMO BACKUP DATABASE AdventureWorks TO DISK = 'C:\AdventureWorks.BAK' GO
Reply Share

Manis h Dubey

Mod

deepak 9 months ago

Y our are right . There are several ways of taking backup of Database using C#, SQL Server scripts etc. Through this article I am not only target to tell about taking Database backup,here I also want to tell my readers about SMO and how you can use its features.
Reply Share

A s hut os h

9 months ago

how its use in Sql Server version 10.50 give rsponse in my Id:- sharanashu88@gmail.com
Reply Share

S alem A L-B adawi

10 months ago

thank you it's great


Reply Share

Manis h Dubey

Mod

Salem AL-Badawi 10 months ago

Thanks Salem
Reply Share

Manis h Dubey

10 months ago

Thanks
Reply Share

V ideo P roduc t ion

10 months ago

Very interesting blog. Thanks for sharing with us.


Reply Share

S udhir DB A K ings

Video Production 6 months ago

Nice post very helpful dbakings


Reply Share

Su b s cri b e

Ad d D i s q u s to yo u r s i te

<< Restore SQL Server Database Using C# Subscribe to: Post Comments (Atom)

Home

SQL Server LOWER Function >>

IF Y OU ENJOY ED THIS POST PLEASE TAKE 5 SECONDS TO SHARE IT.

manish4dotnet.blogspot.in/2013/03/DatabaseBackupusingCSharp.html

5/6

12/28/13
FOLLOWERS

SQL Server Database BackUp using C# | Dot Net World


MUST READ!!! WEEKLY POPULAR POSTS

Join this site


w ith Google Friend Connect

Members (6)

Blogger- Adding Facebook Like Box into Blogger Blog Promoting your blog or web site, Facebook page is a great way. Facebook like box is a widely used social plugin from Facebook developers. F... SQL Server Database BackUp using C# There are various ways to take the SQL Server database backup. You can take the database backup using SQL Server backup wizard or using ... How To- Bind Data to Gridview using jQuery in ASP.Net In this post, I am explaining how to bind data to Gridview using jQuery in ASP.Net through Ajax call. In previous posts, I explained P... Creating Captcha Code in ASP.Net Before entering in to the topic first we must know What is the Captcha code? and Why we used it? Most of the web sites having the Cap... Upload and Read the Excel file using C# This example will show you how to Upload the excel file and then read the excel file data using C# and display it on Gridview. Drag and... Export Gridview to PDF in ASP.Net In web development, Gridview is the most frequently used control for data display in ASP.Net . Sometimes we have a requirement to export ... How To- Search records or data in gridview using jQuery In my previous post, I explained bind the gridview using jQuery . Here I am going to show you how to search record or data with in Gridvie...

SQL Server Database BackUp using C# There are various ways to take the SQL Server database backup. You can take the database backup using SQL Server backup wizard or using ... C#: Difference between throw and throw ex in C# .Net Exception In day to day development, we all are very familiar with Exception handling. We pretty much all know that we should wrap the code which ma... How To- Convert Data Table into List Recently I have posted an article Convert a Generic List to a Datatable . Today I am explaining here the reverse way of my previous artic... How To- Search records or data in gridview using jQuery In my previous post, I explained bind the gridview using jQuery . Here I am going to show you how to search record or data with in Gridvie... ASP.Net- Create Hindi TextBox Using Google Transliteration in ASP.Net Here, I am explaining you how to create a hindi textbox in ASP.Net using Google Transliteration .Here I am mainly targeting english to...

Already a member? Sign in

LISTED IN

search engine info

BlogCatalog
Blog Directory Top Blog Sites Blog Directory Blog Community

Blogs Candles Promote Blog

Jar Candles Blog Submission

HyperSmash http://www.casino-online.us/

GOOGLE+ FOLLOWERS

Manish Dubey
Add to circles

123 have me in circles

View all

Copyright (c) 2013-14 Dot Net World. Powered by Blogger.

hgpromo

manish4dotnet.blogspot.in/2013/03/DatabaseBackupusingCSharp.html

6/6

You might also like