Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL SQL-Server-2008
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);
 
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.
Smile | :)
Posted 27-May-12 22:13pm
saj_211K
Edited 27-May-12 22:36pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I think, You are running SQL Server by Local Account, Change that Kripa aane lage gi Wink | ;) lol
may be an issue of Admin rights thats why
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You need to professional recovery software. Try to use next one to recover .mdf file repair sql database
 
Application will restore your mdf file and save its original structure before extracting
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Gihan Liyanage 332
1 Sergey Alexandrovich Kryukov 290
2 ClimerChinna 222
3 vikinghunter 178
4 OriginalGriff 161
0 Sergey Alexandrovich Kryukov 8,363
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100