I want to select path of restore then select my database backup file and restore it by clicking a button in Visual Studio 2019.
I have some code below which I already have written, but I get an error.
string connection = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
con.Open();
String sqlquery = "Use Master ALTER DATABASE databasename SET OFFLINE WITH ROLLBACK IMMEDIATE RESTORE DATABASE FHMTailorDB FROM DISK ='" + RestoreTextBox.Text + "' ALTER DATABASE databasename SET ONLINE WITH ROLLBACK IMMEDIATE";
SqlCommand cmd = new SqlCommand(sqlquery, con);
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
lblError.Text = "Database Restore successfully";
Error:
System.Data.SqlClient.SqlException: 'User does not have permission to alter database 'databasename', the database does not exist, or the database is not in a state that allows access checks.
ALTER DATABASE statement failed.
Directory lookup for the file "F:\FHMTailorManager\FHMTailorDB.mdf" failed with the operating system error 2(The system cannot find the file specified.).
File 'FHMTailorDB' cannot be restored to 'F:\FHMTailorManager\FHMTailorDB.mdf'. Use WITH MOVE to identify a valid location for the file.
Directory lookup for the file "F:\FHMTailorManager\FHMTailorDB_log.ldf" failed with the operating system error 2(The system cannot find the file specified.).
File 'FHMTailorDB_log' cannot be restored to 'F:\FHMTailorManager\FHMTailorDB_log.ldf'. Use WITH MOVE to identify a valid location for the file. Problems were identified while planning for the RESTORE statement. Previous messages provide details.
RESTORE DATABASE is terminating abnormally.
User does not have permission to alter database 'databasename', the database does not exist, or the database is not in a state that allows access checks.
ALTER DATABASE statement failed.
Changed database context to 'master'.'