在C#中备份数据库 [英] Backup databse in c#

查看:58
本文介绍了在C#中备份数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
请帮助我如何在C#中备份和还原数据库.
我的数据库附加在解决方案中.我在项目中使用了SQLEXPRESS.
谢谢

Hi all
please help me how to Backup&Restore database in C#.
my DB is attached in solution.i used SQLEXPRESS in my project.
Thanks

推荐答案

如果您询问SQL C#数据库问题,请尝试以下代码

Try this code if your asking about SQL C# Database Question

public void BackupDatabase(SqlConnectionStringBuilder csb, string destinationPath)
{
    ServerConnection connection = new ServerConnection(csb.DataSource, csb.UserID, csb.Password);
    Server sqlServer = new Server(connection);

    Backup bkpDatabase = new Backup();
    bkpDatabase.Action = BackupActionType.Database;
    bkpDatabase.Database = csb.InitialCatalog;
    BackupDeviceItem bkpDevice = new BackupDeviceItem(destinationPath, DeviceType.File);
    bkpDatabase.Devices.Add(bkpDevice);
    bkpDatabase.SqlBackup(sqlServer);
    connection.Disconnect();

}

public void RestoreDatabase(String databaseName, String backUpFile, String serverName, String userName, String password)
{
    ServerConnection connection = new ServerConnection(serverName, userName, password);
    Server sqlServer = new Server(connection);
    Restore rstDatabase = new Restore();
    rstDatabase.Action = RestoreActionType.Database;
    rstDatabase.Database = databaseName;
    BackupDeviceItem bkpDevice = new BackupDeviceItem(backUpFile, DeviceType.File);
    rstDatabase.Devices.Add(bkpDevice);
    rstDatabase.ReplaceDatabase = true;
    rstDatabase.SqlRestore(sqlServer);
}



Hirun Meepage



Hirun Meepage


请参阅:

在C#中备份SQL数据库 [ ^ ]

如果您觉得此功能有用,请标记它...
Refer this :

Backing up an SQL Database in C#[^]

Mark it if u find this useful.....


这篇关于在C#中备份数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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