I have an ASP.NET web form which have three buttons Backup, Reset and Restore Database. 
When i click BackUp, it start backup of database and create a .bak file. The Reset button truncate all tables. But the Restore database will act as Undo i.e when i click Restore, it will restore the same database again from .bak file.
Currently, when i click restore it says that the Database is in Use and i cannot restore it. For this, I thought to detach the database first and then restore it with same name from .bak file.
My Idea is to connect to master database and from there i will detach my database and then restore it from .bak file. But I am not sure, if it is a good idea or will it work.
I used the following to restore the Database but I want to restore it with same name.
    SQLcon.ConnectionString = "Data Source=" + servername + ";Initial Catalog=master;pwd=" + pwd + ";user=" + user + ";"
    SQLcon.Open()
    Try
       SQLCmd = New SqlCommand("Restore database MyDatabaseNew from disk='E:\MyDatabase.bak' WITH MOVE 'MyDatabase_Data' TO 'D:\DATA\MyDatabase .mdf', MOVE 'MyDatabase_Log' TO 'D:\DATA\MyDatabaseLog.ldf',REPLACE,STATS=10", SQLcon)
        SQLCmd.CommandTimeout = CInt(CommandTimeout)
        SQLCmd.ExecuteNonQuery()
        Response.Write("restore database successfully")
How i will achieve this? Any suggestion will be appreciated.