Taking SQL Server backup through code

I am using visual studio 2008 and dotnet framework 2.0 to develop a web application using asp.net and c#.
I am using microsoft sql server 2005 express edition as database.  I have to take backup of this database on a button click from the application. can anyone suggest the best method for doing this.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale BurrellDirectorCommented:
Something like this should do the job.
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

String InstanceName = "MyServerName";
String DatabaseName = "MyDatabaseName";
String BakFileName = "MyDatabaseBackupName";

ServerConnection Conn = new ServerConnection(InstanceName);
Conn.LoginSecure = true;
Server SqlServer = new Server(Conn);
Backup DatabaseBackup = new Backup();
// Set the backup type to a database backup
DatabaseBackup.Action = BackupActionType.Database;
// Set the database that we want to perform a backup on
DatabaseBackup.Database = DatabaseName;

// Set the backup device to a file
BackupDeviceItem BackupDevice = new BackupDeviceItem(MyDatabaseBackupName, DeviceType.File);
// Add the backup device to the backup
// Perform the backup

Open in new window

10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Make sure you are using System.Data.SqlClient;

Sql client should be on most machines, but DMO may not be available (and may require higher permissions).  When the button is clicked, these two lines will do it.
For the syntax of the full "BACKUP DATABASE" command, have a look at books online.

SqlConnection conn = new SqlConnection("a connection string, same as your asp.net app");
SqlCommand cmd = new SqlCommand("backup database mydb to disk='c:\path\file.bak'", conn);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
renilccAuthor Commented:
hello cyberkiwi,

your comment was excellent and it worked fine in local machine. But when i uploaded the application, the applicatio tried to create a backup on the server itself and returned error. How could i create a backup in my local machine from server?
Dale BurrellDirectorCommented:
You can only backup to a path that is available to the server and that the server has permissions to write to. Often with web servers for example you need to backup locally and then copy it elsewhere, potentially after compressing it.
Yes SQL Server will only back up to the local server, unless a special trace flag is set which allows it network access (and even then you need to run the SQL Server service under networkservice at least).  If you are on SQL Server 2008, you can add with compression to the backup command to make the file smaller to copy.  If you have access to the SQL server's file shares, you could add code in the .Net application to transfer the file.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.