Taking SQL Server backup through code

Hello,
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.
renilccAsked:
Who is Participating?
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
DatabaseBackup.Devices.Add(BackupDevice);
// Perform the backup
DatabaseBackup.SqlBackup(SqlServer);

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

cyberkiwiCommented:
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);
cmd.ExecuteNonQuery();
0

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?
0
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.
0
cyberkiwiCommented:
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.
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.