数据库还原失败 [英] Restore of Database Failed

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

问题描述

当我尝试还原数据库时出现错误-

when i am trying to restore database it gives a error-

User does not have permission to alter database 'C:\Temp\DB_Jaggry.bak', or the database does not exist.
ALTER DATABASE statement failed.



还原数据库的代码-



Code for Restore database-

void Restore(string backUpPath)
        {
            SqlConnection con = new SqlConnection(@"Data Source=SNEHA-PC\SQLEXPRESS;Initial Catalog=DB_Jaggry;Integrated Security=True");
            
                con.Open();
                string DatabaseFullPath = @"C:\Temp\DB_Jaggry.bak";
                string UseMaster = "USE master";
                SqlCommand UseMasterCommand = new SqlCommand(UseMaster, con);
                UseMasterCommand.ExecuteNonQuery();

                string Alter1 = @"ALTER DATABASE [" + DatabaseFullPath + "] SET Single_User WITH Rollback Immediate";
                SqlCommand Alter1Cmd = new SqlCommand(Alter1, con);
                Alter1Cmd.ExecuteNonQuery();

                string Restore = @"RESTORE DATABASE [" + DatabaseFullPath + "] FROM DISK = N'" + backUpPath + @"' WITH  FILE = 1,  NOUNLOAD,  STATS = 10";
                SqlCommand RestoreCmd = new SqlCommand(Restore, con);
                RestoreCmd.ExecuteNonQuery();

                string Alter2 = @"ALTER DATABASE [" + DatabaseFullPath + "] SET Multi_User";
                SqlCommand Alter2Cmd = new SqlCommand(Alter2, con);
                Alter2Cmd.ExecuteNonQuery();
                MessageBox.Show("Restore data succesfully");
                           
        }


当我将Databasefullpath更改为"DB_Jaggry"时,它没有给出错误,但是当我检查数据库时,我无法还原backUpPath的数据库
请帮助我!


when i am changed the Databasefullpath to "DB_Jaggry" it does not give a error but when i check the database i can''t restore the database of backUpPath
Plz help me !

推荐答案

使用以下行:
Use the following line:
SqlConnection con = new SqlConnection(@"Data Source=SNEHA-PC\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");


您无法连接到不存在的数据库,请首先连接到主数据库,然后还原数据库.


You cannot connect to a database which does not exist, connect to the master database first then restore the DB.


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

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