简体   繁体   English

C#SQL Server Express备份和还原

[英]C# SQL Server Express backup and restore

Basically I have a windows form C# program that uses a SQL Server Express database to store data entered by the users. 基本上我有一个Windows窗体C#程序,它使用SQL Server Express数据库来存储用户输入的数据。 I want to add the ability for the user to be able to backup their database and then load a backup or another database into the programs database. 我想为用户添加能够备份其数据库,然后将备份或其他数据库加载到程序数据库中的功能。

If someone could point me in the right direction like an article or tutorial that would be great, because I have hit a dead end. 如果有人能指出我正确的方向,就像文章或教程那样伟大,因为我已经走到了尽头。

Thanks 谢谢

You have a few options to do this: 您有几个选项可以执行此操作:

  1. On the server directly call BACKUP DATABASE , RESTORE DATABASE commands. 在服务器上直接调用BACKUP DATABASERESTORE DATABASE命令。

  2. Use SMO ( SQL Server Management Objects ) to perform the backup restore operations programatically. 使用SMO( SQL Server管理对象 )以编程方式执行备份还原操作。 (Specifically this sample ) (特别是这个样本

  3. Use some third party utility/library. 使用一些第三方实用程序/库。

The nice thing about SQL Server is that you can do more or less everything with SQL (T-SQL or some variation thereof) - this is nice because it means that it is relatively easy to do most things without having to worry about (for example) the availability of SMO and further in a reasonably generic fashion. 关于SQL Server的好处是你可以使用SQL(T-SQL或其中的一些变体)做更多或更少的事情 - 这很好,因为这意味着做大多数事情相对容易而不必担心(例如)SMO的可用性以及合理的通用方式。

The second nice thing is that if you run a command in SQL Server Management Studio (a version of which can be downloaded for SQL Server Express) it will, more often than not (and certainly in the case of Backup and Restore), offer to let you save the the script to a file instead of executing it. 第二件好事是,如果你在SQL Server Management Studio中运行一个命令(其中一个版本可以为SQL Server Express下载),那么它(通常在备份和恢复的情况下)通常会提供给让您将脚本保存到文件而不是执行它。

So, it would be straightforward for you to use SQL Server Management Studio to determine the structure of SQL based backu and resotre commands and from there to integrate those into your application. 因此,您可以直接使用SQL Server Management Studio来确定基于SQL的backu和resotre命令的结构,并从那里将它们集成到您的应用程序中。

There is, however, a "gotcha" - SQL server will only save backups to/restore backups from a drive that is visible to the machine & service account upon which the SQL Server instance is running, almost certainly not an issue if your application is running on the same machine as the user but potentially a problem if they are not. 但是,有一个“问题” - SQL服务器只会将备份保存到/运行SQL Server实例运行的机器和服务帐户可见的驱动器上的备份,如果您的应用程序是与用户在同一台​​机器上运行,但如果不是,可能会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM