Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Folks,

I want to take the SQL Server 2005 backup using ASP.NET 2.0 & C#. Please provide the way for it.

Many thanks
Posted
Updated 2-Feb-10 10:59am

Hi,

Check below link it will help you.

http://www.asp101.com/articles/carvin/sqldmobackup/default.asp
 
Share this answer
 
Use the inbuilt backup capabilities of SQL Server through a maintenance plan if you need a backup to occur on a schedule, which is better than having to get a user to remember to do it.

However if you feel you want to run it through code use the folowing sql command;
BACKUP DATABASE [DatabaseName] TO  DISK = 
    N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\DatabaseName_backup_201002021714.bak' WITH NOFORMAT, 
    NOINIT,  NAME = N'DatabaseName_backup_20100202171425', SKIP, 
    REWIND, NOUNLOAD,  STATS = 10

Just remember to replace "DatabaseName" with the name of the database to backup.
 
Share this answer
 
v2
There are many ways to do this:

1. Using SMO (Management Objects)

using Microsoft.SqlServer.Management.Smo;

Server svr = new Server();
Backup bkp = new Backup();
bkp.Devices.AddDevice(@"C:\YourDatabase.bak", DeviceType.File);
bkp.Database = "YourDATABASE";
bkp.Action = BackupActionType.Database;
bkp.Initialize = true;
bkp.SqlBackup(svr);


This is the easiest way. But you need the reference to SMO objects.

2. Use Backup script to do this :

string sql = "BACKUP DATABASE [YourDatabase] TO  DISK = N'C:\YourDatabase.bak' WITH NOFORMAT, NOINIT,  NAME = N'YourDatabase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10"

using(SqlConnection con = new SqlConnection(masterconstring))
{
   SqlCommand cmd = new SqlCommand(sql);
   cmd.ExecuteNonQuery();
}


In this case you can connect normally to the master database and run the backup script.

:thumbsup:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900