问题描述
我已经尝试在程序上启用还原和备份数据库功能
并且我已经在此链接上使用代码来基于备份/还原我的项目进行测试
SqlServer备份/还原实用程序 [ ^ ]
我已经从链接加载了程序演示,它也可以备份和还原我的数据库
将源复制到项目功能中后,可以备份.我也可以备份数据库
但是当我尝试还原时,我得到了错误服务器"PETCHYPC-pc \ sqlexpress"的还原失败"
我只是已经更改了我的连接字符串,但是为什么我遇到了这个问题
这是我的代码
I''ve try to make Restore and backup database function on my program
and I''ve use code on this link to based of my backup/restore my project for testing
SqlServer Backup/Restore Utility[^]
I''ve Load Program demo from link and it''s can backup and restore my database as well
When I''ve copy source to my project function Back up is ok. I can backup my database as well
but when i''ve try to restore i''ve got ERROR "Restore failed for Server ''PETCHYPC-pc\sqlexpress''"
I just already change my connection string yet but why I''ve got this problem
this''s my code
Database restoreDb = (Database)cmbRestoreDb.SelectedItem;
dbName = restoreDb.Name;
Restore dbRestore = new Restore();
dbRestore.Database = restoreDb.Name;
dbRestore.Action = RestoreActionType.Database;
dbRestore.ReplaceDatabase = true;
string fileLocation = ConfigurationManager.AppSettings["SqlFileLocations"];
try
{
BackupDeviceItem device = new BackupDeviceItem(openBakFile.FileName, DeviceType.File);
dbRestore.Devices.Add(device);
DataTable dtFiles = dbRestore.ReadFileList(sqlServer);
string backupDbLogicalName = dtFiles.Rows[0]["LogicalName"].ToString();
RelocateFile dbRf = new RelocateFile(backupDbLogicalName, string.Format("{0}\\{1}.mdf", fileLocation, dbName));
RelocateFile logRf = new RelocateFile(string.Format("{0}_log", backupDbLogicalName), string.Format("{0}\\{1}_Log.ldf", fileLocation, dbName));
dbRestore.RelocateFiles.Add(dbRf);
dbRestore.RelocateFiles.Add(logRf);
string sql = string.Empty;
StringCollection scriptColl = dbRestore.Script(sqlServer);
foreach (string str in scriptColl)
{
sql += str;
}
progress2.Visible = true;
progress2.Value = 0;
dbRestore.Complete += new ServerMessageEventHandler(dbRestore_Complete);
dbRestore.PercentComplete += new PercentCompleteEventHandler(PercentComplete2);
dbRestore.SqlRestore(sqlServer);
}
catch (Exception exc)
{
dbRestore.Abort();
MessageBox.Show(string.Format("Exception occured.\nMessage: {0}", exc.Message));
谁能帮我
附言对不起我的英语不好 .现在我已经练习使用它
Can anyone to help me
Ps. Sorry for my English . Now I''ve practice to use it
推荐答案
这篇关于关于从C#Winapp还原数据库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!