如何从 vb.net 应用程序备份和恢复数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13881758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take backup and restore data from vb.net application
提问by Raj Kashyap
I am developing a desktop application in vb.net for student details management.
我正在 vb.net 中开发用于学生详细信息管理的桌面应用程序。
In that I want to add feature of backup and restore the data. By using this feature user should be able to take data backup and when he wants he should be able to restore data again (if system crash occurs) so data will not be lost.
我想添加备份和恢复数据的功能。通过使用此功能,用户应该能够进行数据备份,并且在需要时他应该能够再次恢复数据(如果发生系统崩溃),因此数据不会丢失。
How to do that using vb.net code, I am using VS2008.
如何使用 vb.net 代码做到这一点,我使用的是 VS2008。
回答by Ciarán
You can execute BACKUP & RESTORE commands just the same way as any other via vb.net, so something like this would do it...
您可以通过 vb.net 以与执行任何其他命令相同的方式执行 BACKUP & RESTORE 命令,所以像这样的事情就可以做到...
Dim sqlConn As New SqlConnection("Data Source=(local);Initial Catalog=master;User ID=sa;Trusted_Connection=true")
sqlConn.Open()
Dim sCommand = "BACKUP DATABASE [YourDatabaseName] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\Backup.bak' WITH COPY_ONLY"
Using sqlCmd As New SqlCommand(sCommand, sqlConn)
sqlCmd.ExecuteNonQuery()
End Using
Where obviously your database name would replace [YourDatabaseName]. The equivalent RESTORE command would be something like this...
显然您的数据库名称将替换 [YourDatabaseName]。等效的 RESTORE 命令将是这样的......
sCommand = "RESTORE DATABASE [YourDatabaseName] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\TestBackup.bak' WITH REPLACE"
Using sqlCmd As New SqlCommand(sCommand, sqlConn)
sqlCmd.ExecuteNonQuery()
End Using
You would need to review the documentation for these commands to tailor them to your specific scenario.
您需要查看这些命令的文档以根据您的特定场景定制它们。

