For reliability first I copy locally a .bak file from the network and then try to restore it to the local SQL Server instance. However the restore wants to restore the DB file to the original folder location which doesn't exist locally. How can I tell it to use the local SQL instance default file directory?
public void RestoreDB(string file)
{
try
{
SqlConnection sqlConnection = new SqlConnection()
{
ConnectionString = $"Data Source=(LocalDB)\\MSSQLLocalDB;User ID=xx;Password=xx"
};
ServerConnection serverConnection = new ServerConnection(sqlConnection);
Server dbServer = new Server(serverConnection);
Restore restore = new Restore()
{
Action = RestoreActionType.Database,
Database = "DBTest",
NoRecovery = false,
ReplaceDatabase = true
};
restore.Devices.AddDevice(file, DeviceType.File);
restore.SqlRestore(dbServer);
}
catch (Exception ex)
{
throw ex;
}
}