Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Cursor.Current = Cursors.WaitCursor;

           try
           {
               if (txtFileName.Text == "")
               {
                   MessageBox.Show("Please Select Restore File.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                   txtFileName.Focus();
                   return;
               }
               FileInfo file = new FileInfo(txtFileName.Text);
               if (file.Exists)
               {
                   if (MessageBox.Show("Are you sure you restore?", "Back", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                   {
                       if (file.Extension == ".mbk")
                       {

}
else
{

FileInfo filebak = new FileInfo(txtFileName.Text);

String Conn = File.ReadAllText("ConnectionString.txt");
SqlConnection _SqlConnection = new SqlConnection(Conn);
_SqlConnection.Open();
//_SqlCommand = new SqlCommand("use master", connect);
//_SqlCommand.ExecuteNonQuery();
_SqlCommand = new SqlCommand("USE master ALTER DATABASE " + "PragatiDB" + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE; ALTER DATABASE " + "PragatiDB" + " SET MULTI_USER;", _SqlConnection);
_SqlCommand.ExecuteNonQuery();
_SqlCommand = new SqlCommand(@"restore database " + "PragatiDB" + " from disk = '" + BackupPath_bak + "\\" + filebak.Name + "' WITH REPLACE", _SqlConnection);
_SqlCommand.ExecuteNonQuery();
_SqlConnection.Close();
//Directory.Delete(BackupPath_bak, true);


}
MessageBox.Show("Database " + "PragatiDB" + " Restored successfully.", "Restoration", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
MessageBox.Show(@"Do not make any endorsement above (or is not in the correct path)", "Restoration", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
catch (Exception exp)
{
MessageBox.Show(exp.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
Posted
Comments
BillWoodruff 10-Feb-14 2:58am    
Please do not repost: instead, revise your original post with the same, or similar, title.

It's also important to ask specific questions: don't make us try and guess what your question is from the title of your post.

1 solution

try this:
C#
string filename='D:\BK\'+db_name+'.bak'
_SqlCommand = new SqlCommand(@"restore database " + "PragatiDB" + " from disk = "'+filename+'" WITH REPLACE", _SqlConnection);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900