如何从SQLEXPRESS备份和恢复 [英] How to backup and restore from SQLEXPRESS
问题描述
Hi
我使用下面的代码从SQLEXPRESS备份
Hi
I used the code below to backup from SQLEXPRESS
SqlConnection con = new SqlConnection(@"Data Source=.\SQLExpress;User Instance=true;Integrated Security=True;Initial Catalog=[myDb];AttachDBFilename='" + DbPath + "'");
SqlCommand com = new SqlCommand("BACKUP DATABASE [myDb] TO DISK = N'" + BackupPath + "'",con);
com.Connection.Open();
com.ExecuteNonQuery();
com.Connection.Close();
但是我得到了这个例外:
数据库'myDb'不存在。确保输入的名称正确。
BACKUP DATABASE异常终止。
当我将sqlconnection更改为:
But I get this exception :
Database 'myDb' does not exist. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.
And when I change sqlconnection to this:
SqlConnection con = new SqlConnection(@"Data Source=.\SQLExpress;User Instance=true;Integrated Security=True;Initial Catalog=[myDb];AttachDBFilename='" + DbPath + "';database=myDb");
我得到这个例外:
无法打开物理文件C:\Users\MohammadHG\Documents\Visual工作室2010\Projects\WindowsFormsApplication1\bin\Debug\Database\ db.mdf。操作系统错误32:32(无法检索此错误的文本。原因:15105)。
无法附加文件'C:\Users\MohammadHG \Documents\ Visual Studio 2010 \Projects\WindowsFormsApplication1\bin\Debug\Database\db.mdf'作为数据库'myDb'。
我用来连接和执行SQLCommands整个我的应用程序没有任何问题,我只是有备份和恢复问题。
请帮助我...
I get this exception :
Unable to open the physical file "C:\Users\MohammadHG\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\bin\Debug\Database\db.mdf". Operating system error 32: "32(failed to retrieve text for this error. Reason: 15105)".
Cannot attach the file 'C:\Users\MohammadHG\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\bin\Debug\Database\db.mdf' as database 'myDb'.
I used to connect and execute SQLCommands allover my application without any problems, I just have problem with backup and restore.
please help me...
推荐答案
如果你正在尝试恢复不存在的数据库,然后您的连接字符串不应该连接到它,请尝试连接到master
。
If you are trying to restore a database that does not exist then your connection string should not be connecting to it, try connecting tomaster
instead.
SqlConnection con = new SqlConnection(@"Data Source=.\SQLExpress;User Instance=true;Integrated Security=True;Initial Catalog=master");
这篇关于如何从SQLEXPRESS备份和恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!