关于从C#Winapp还原数据库的问题 [英] Question about Restore database from C# Winapp

查看:105
本文介绍了关于从C#Winapp还原数据库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试在程序上启用还原和备份数据库功能

并且我已经在此链接上使用代码来基于备份/还原我的项目进行测试

SqlServer备份/还原实用程序 [
我只是已经更改了我的连接字符串,但是为什么我遇到了这个问题

这是我的代码

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

推荐答案

我已经有了解决方案

我在连接字符串中输入了"Initial Catalog = master",效果很好
I''ve got the solution

i put "Initial Catalog=master" to my connectionstring and it''s work good


这篇关于关于从C#Winapp还原数据库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆