I am trying to restore sql databse using C# code.Backup is working fine. But when the restore the db it gives me an error.
I am using the `Microsoft.SqlServer.Management.Smo;` for this operation.
The error is
{"System.Data.SqlClient.SqlError: RESTORE cannot process database 'TempDb' because it is in use by this session. 
It is recommended that the master database be used when performing this operation."}

In several posts it says set the database to the master. I also tried that.But it gives me the same error.
Connection string:connectionString = @"server=(local);Initial Catalog=Master;Integrated Security=True;";

My code is as follows:

```
openFileDialog1.ShowDialog();
        string databaseName = "TempDb";
        Restore sqlRestore = new Restore();

        BackupDeviceItem deviceItem = new BackupDeviceItem(openFileDialog1.FileName, DeviceType.File);
        sqlRestore.Devices.Add(deviceItem);
        sqlRestore.Database = databaseName;

        DataConnection dataConnection = new DataConnection();
        ServerConnection connection = new ServerConnection(dataConnection.DataBaseConnection);
        Server sqlServer = new Server(connection);

        Database db = sqlServer.Databases[databaseName];
        sqlRestore.Action = RestoreActionType.Database;
        String dataFileLocation = db.FileGroups[0].Files[0].FileName;
        String logFileLocation = db.LogFiles[0].FileName;
        db = sqlServer.Databases[databaseName];
        RelocateFile rf = new RelocateFile(databaseName, dataFileLocation);

        sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation));
        sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation));
        sqlRestore.ReplaceDatabase = true;
        sqlRestore.Complete += new ServerMessageEventHandler(sqlRestore_Complete);
        sqlRestore.PercentCompleteNotification = 10;
        sqlRestore.PercentComplete += new PercentCompleteEventHandler(sqlRestore_PercentComplete);
        sqlRestore.SqlRestore(sqlServer);
        db = sqlServer.Databases[databaseName];
        db.SetOnline();
        sqlServer.Refresh();

```