I have developed a MIS in Windows Forms in which I took backup of my SQL Server 2012 database, but I am unable to restore the backup (.bak
) file.
This is my code:
private void buttonRestore_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Filter = "Backup File |*.bak";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string sql = "Alter Database BOQ SET SINGLE_USER WITH ROLLBACK IMMEDIATE;";
sql += "Restore Database BOQ FROM DISK ='" + openFileDialog1.FileName + "' WITH REPLACE;";
SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=BOQ;Integrated Security=True");
SqlCommand command = new SqlCommand(sql,con);
con.Open();
command.ExecuteNonQuery();
MessageBox.Show("Database Recovered Successfully!");
con.Close();
con.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
But I get this error:
You cannot connect to your database BOQ
and then restore a .bak
over top of it!
"Data Source=.; Initial Catalog=BOQ;Integrated Security=True"
****
You need to connect to the master
database, and then you can restore your BOQ
database:
"Data Source=.; Initial Catalog=master;Integrated Security=True"
*******
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With