以编程方式备份​​SQL Server 2005数据库 [英] SQL Server 2005 database backup progammatically

查看:79
本文介绍了以编程方式备份​​SQL Server 2005数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想使用ASP.NET 2.0和C#进行SQL Server 2005备份.请提供解决方法.

非常感谢

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

推荐答案



检查下面的链接将为您提供帮助.

http://www.asp101.com/articles/carvin/sqldmobackup/default.asp
Hi,

Check below link it will help you.

http://www.asp101.com/articles/carvin/sqldmobackup/default.asp


如果需要进行备份,请通过维护计划使用SQL Server的内置备份功能.日程安排,这比让用户记住要执行的时间表要好.

但是,如果您想通过代码运行它,请使用以下sql命令;
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


只要记住用要备份的数据库名称替换"DatabaseName"即可.


Just remember to replace "DatabaseName" with the name of the database to backup.


有很多方法可以做到这一点:

1.使用SMO(管理对象)

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);



这是最简单的方法.但是您需要引用SMO对象.

2.使用备份脚本执行此操作:



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();
}



在这种情况下,您可以正常连接到master数据库并运行备份脚本.

:thumbsup:



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

:thumbsup:


这篇关于以编程方式备份​​SQL Server 2005数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆