还原SQL Express数据库备份 [英] Restore SQL Express database backup

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

问题描述

亲爱的朋友,

我正在开发Windows应用程序,我想在其中开发一个还原实用程序,其中有一个按钮,然后单击该按钮,我正在创建一个新数据库并还原给定的备份文件.

我的代码如下

Dear friends,

I am developing a windows application in which i want to develop a restore utility in which i have a button and on click of that button i am creating a new database and restoring the given backup file.

my code is as follows

private void Restore_Click(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection(@"Data Source=.\SQLExpress;Integrated Security=True");
       con.Open();
       DateTime nwDt = DateTime.Now;
       string strWinPath = Environment.GetEnvironmentVariable("windir");
       string strTemp = strWinPath.Substring(0, 1);

       StringBuilder sbOGDatabasePath = new StringBuilder(@"X:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\");

       sbOGDatabasePath.Replace('X', strTemp[0], 0, 1);



       string path = Environment.CurrentDirectory.ToString();

       string frmt = "DdMMMyyThTmm";
       string chngdt = nwDt.ToString(frmt);
       string sql = "CREATE DATABASE mydb2" + chngdt + "";

       string clientMDFPath = sbOGDatabasePath.ToString() + "mydb2" + chngdt + ".mdf";
       string clientLDFPath = sbOGDatabasePath.ToString() + "mydb2" + chngdt + ".ldf";


       SqlCommand cmd = new SqlCommand(sql, con);
       try
       {
           cmd.ExecuteNonQuery();

           MessageBox.Show("Database created");
           con.Close();
           con.Open();
       }

       catch (SqlException ex)
       {
           MessageBox.Show(ex.Message.ToString());
       }
       con.Close();

       SqlConnection Connection = new SqlConnection(@"Data Source=.\SQLExpress;Integrated Security=True");


       ServerConnection srvConn = new ServerConnection(Connection);
       Server srvSql = new Server(srvConn);


       if (srvSql != null)
       {

           if (openBackupDialog.ShowDialog() == DialogResult.OK)
           {

               Restore rstDatabase = new Restore();

               rstDatabase.Action = RestoreActionType.Database;

               rstDatabase.Database = "mydb2" + chngdt + "";
               string dbNm = "mydb2" + chngdt + "";
               string dbLogNm = "mydb2" + chngdt + "_Log";

               rstDatabase.RelocateFiles.Add(new RelocateFile(dbNm, clientMDFPath));

               rstDatabase.RelocateFiles.Add(new RelocateFile(dbLogNm, clientLDFPath));


               BackupDeviceItem bkpDevice = new BackupDeviceItem(openBackupDialog.FileName, DeviceType.File);



               rstDatabase.Devices.Add(bkpDevice);



               rstDatabase.ReplaceDatabase = true;


               Connection.Open();

               rstDatabase.SqlRestore(srvSql);


               string sqlOn = "alter database mydb2" + chngdt + " set online with rollback immediate";
               SqlCommand cmdOn = new SqlCommand(sqlOn, Connection);
               try
               {
                   cmdOn.ExecuteNonQuery();

               }
               catch (Exception esp)
               {
               }
               MessageBox.Show("Database restored");
           }

       }

       else
       {
           MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
       }

   }



它在以下代码上给我一个错误:



It gives me an error on following code:

rstDatabase.SqlRestore(srvSql);



错误:服务器"SHAILESH-LP \ SQLExpress"的恢复失败.

InnerException : {文件\"C:\\ Demo \\ mysql \\ mydb1_data.mdf \"的目录查找失败,并出现操作系统错误3(系统找不到路径\ r \ n文件"test_data"无法还原到"C:\\ Demo \\ mysql \\ mydb1_data.mdf".使用WITH MOVE标识文件的有效位置.对文件\"C:\\ Demo \\ mysql \\ mydb1_log.ldf \"的nDirectory查找失败,出现操作系统错误3(系统找不到指定的路径.).还原到"C:\\ Demo \\ mysql \\ mydb1_log.ldf".使用WITH MOVE标识文件的有效位置.\ r \ n在计划RESTORE语句时发现了问题.以前的消息提供了详细信息.\ r \ nRESTORE DATABASE异常终止.}

有人可以帮我解决这个问题吗?

提前谢谢.
Shailesh J.
:-)



Error : Restore failed for Server ''SHAILESH-LP\SQLExpress''.

InnerException : {"Directory lookup for the file \"C:\\Demo\\mysql\\mydb1_data.mdf\" failed with the operating system error 3(The system cannot find the path specified.).\r\nFile ''test_data'' cannot be restored to ''C:\\Demo\\mysql\\mydb1_data.mdf''. Use WITH MOVE to identify a valid location for the file.\r\nDirectory lookup for the file \"C:\\Demo\\mysql\\mydb1_log.ldf\" failed with the operating system error 3(The system cannot find the path specified.).\r\nFile ''mydbb_log'' cannot be restored to ''C:\\Demo\\mysql\\mydb1_log.ldf''. Use WITH MOVE to identify a valid location for the file.\r\nProblems were identified while planning for the RESTORE statement. Previous messages provide details.\r\nRESTORE DATABASE is terminating abnormally."}

Can anybody help me to solve this...

Thanks in advance.
Shailesh J.
:-)

推荐答案

我认为,您正在通过本地帐户运行SQL Server,请更改该名称;)哈哈
可能是管理员权限的问题,这就是为什么
I think, You are running SQL Server by Local Account, Change that Kripa aane lage gi ;) lol
may be an issue of Admin rights thats why


系统找不到指定的路径.
错误明确表明为MDF文件指定的文件路径无效,并且该位置不存在该文件.

您已使用它来获取路径:Environment.CurrentDirectory.ToString();.它为您提供网站的根目录.
要么将MDF放置在此位置,要么提供必须从中拾取MDF的正确路径.
The system cannot find the path specified.
Error clearly speaks that the file path specified for the MDF file is invalid and the file does not exists at that location.

You have used this for getting the path: Environment.CurrentDirectory.ToString(); . It gives you the websites root directory.
Either, place the MDF to be resotredin this location OR provide a correct path from where the MDF has to be picked.


^ ]

http://reference7.blogspot.com/2012/05/database-backup- using-c-code.html [ ^ ]
http://reference7.blogspot.com/2012/05/restore-database-using-c-code.html[^]

http://reference7.blogspot.com/2012/05/database-backup-using-c-code.html[^]


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

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