简体   繁体   English

如何在独立的MDF中还原和备份数据

[英]How to restore and backup data in standalone mdf

I am developing a windows form application in c# visual studio 2010, where I want to create a feature in which a user is allowed to restore and backup the database by itself. 我正在c#visual Studio 2010中开发Windows窗体应用程序,我想在其中创建一个功能,允许用户自己还原和备份数据库。 The problem is that I am using a standalone mdf file. 问题是我正在使用独立的mdf文件。

i am using sql server express by attaching mdf file to the application, when i try to backup using query it's work but when i try restore the database using query it say that it's say that doesn't have permission to alter table. 我正在通过将mdf文件附加到应用程序来使用sql server express,当我尝试使用查询进行备份时是可行的,但是当我尝试使用查询来恢复数据库时,即表示没有更改表的权限。 And then i try to using smo but it say that unable to open the file. 然后我尝试使用smo,但是它说无法打开文件。 So i am wondering if there is any option 所以我想知道是否有任何选择

To create a backup using t-sql you can use: 要使用t-sql创建备份,可以使用:

BACKUP DATABASE AdventureWorks 
TO DISK = 'C:\BACKUP\AdventureWorks.bak'
GO

To restore use: 恢复使用:

RESTORE DATABASE AdventureWorks FROM DISK = 'C:\BACKUP\AdventureWorks.bak'
GO

Add WITH REPLACE to replace the existing database. 添加WITH REPLACE替换现有数据库。

You can fire those SQL commands using ExecuteNonQuery . 您可以使用ExecuteNonQuery触发这些SQL命令。

Note: this works on Standard SQL Server, but I have not used it on SQL Server Express with an attached database file. 注意:这在标准SQL Server上有效,但是我没有在带有附加数据库文件的SQL Server Express上使用它。

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

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