bugfix> c# > 投稿

SQL Server 2012からデータベースを復元する方法は?

これは私のコードです:

private void [connTes()][1]
{
        try
        {
            conString = "server=.\\SQLEXPRESS;database=db_datatestproject;user=admin;password=123;Integrated Security=True";
            connnn = new SqlConnection(conString);
            connnn.Open();
        }
        catch
        {
        }
}
private void button1_Click(object sender, EventArgs e)
{
        connTes();
        try
        {
            if (txtlocation.Text == "")
            {
                MessageBox.Show("select database");
                return;
            }
            else
            {
                string databesing = connnn.Database.ToString();
                string a = "ALTER DATABASE " + databesing + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE;";
                a += "RESTORE DATABASE "+databesing+" FROM DISK ='"+txtlocation.Text+"' WITH REPLACE;";
                SqlCommand cmd = new SqlCommand(a, connnn);
                SqlDataReader dr = cmd.ExecuteReader();
                connnn.Close();
                connnn.Dispose();
                MessageBox.Show("done restored");
            }
        }
        catch(SqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
}

これは私が得るエラーです:

RESTORE cannot process database 'db_testproject' because it use by the session. It is recommended that the master database be used when performing this operation. RESTORE DATABASE is terminating abnormally.

このエラーを修正するには?

どんな助けも大歓迎です。

ありがとうございました。