I have a form to restore a database backup, when user select file.bak
and clicks on a Restore
button, this message shows up:
and my database in SQL Server changes to Single User
mode like that
I use Windows authentication
My code
///// declare some var in partial class
public partial class RestoewDB : Form
{
private string connectionString = @"server=.\SQLEXPRESS;DataBase=StoreDataBase; Integrated Security=true";
private SqlConnection conn;
private SqlCommand command;
//////////////
// my code when user select file.bak and click restore button
/////////////
try
{
string DatabaseName = "StoreDataBase";
conn = new SqlConnection(connectionString);
conn.Open();
string sql = "Alter Database " + DatabaseName + " Set SINGLE_USER WITH ROLLBACK IMMEDIATE; ";
sql += "Restore Database " + DatabaseName + " FROM Disk ='" + textBox1.Text + "'; ";
command = new SqlCommand(sql, conn);
command.ExecuteNonQuery();
textBox1.Text = "";
MessageBox.Show("Successfully Restored Database.");
conn.Close();
conn.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}