Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

I am trying to restore a db with Microsoft.SqlServer.Management.Smo but I keep getting access denied.

I have set up the service user for the instance (MSSQL$LOCAL2016) with full access rights to the folder
The folder is the default folder for the instance (although the same cannot be said when I move to production)
I create the folders within C#
rstDatabase.SqlRestore(sqlServer); fails with the error:
Quote:
The operating system returned the error '5(Access is denied.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Database\Rates.setuptest\Data'.
File 'RateUploader' cannot be restored to 'C:\Database\Rates.setuptest\Data'. Use WITH MOVE to identify a valid location for the file.
The operating system returned the error '5(Access is denied.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Database\Rates.setuptest\Logs'.
File 'RateUploader_log' cannot be restored to 'C:\Database\Rates.setuptest\Logs'. 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.



Seems straight forward enough. Am I missing the obvious or is there a subtly I am not aware off?

Thanks ^_^
Andy

What I have tried:

C#
public static bool RestoreDatabase(String databaseName)
{
	MySqlConnectionStringBuilder stringBuilder = new MySqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["adminSqlAccess"].ConnectionString);
	
	string serverName = stringBuilder.Server, userName = stringBuilder.UserID, password = stringBuilder.Password, 
		pFileLocation = string.Format(ConfigurationManager.AppSettings["dbPath"], databaseName, "Data"), 
		lFileLocation = string.Format(ConfigurationManager.AppSettings["dbPath"], databaseName, "Logs"),
		bakFileLocation = ConfigurationManager.AppSettings["bakDbPath"];
		
	if (!Directory.Exists(pFileLocation))
		Directory.CreateDirectory(pFileLocation);
	if (!Directory.Exists(lFileLocation))
		Directory.CreateDirectory(lFileLocation);

	ServerConnection connection = new ServerConnection(serverName, userName, password);
	Server sqlServer = new Server(connection);
	Restore rstDatabase = new Restore();
	rstDatabase.Action = RestoreActionType.Database;
	rstDatabase.Database = $"Rates.{databaseName}";

	rstDatabase.RelocateFiles.Add(new RelocateFile("RateUploader", pFileLocation));
	rstDatabase.RelocateFiles.Add(new RelocateFile("RateUploader_log", lFileLocation));

	BackupDeviceItem bkpDevice = new BackupDeviceItem(bakFileLocation, DeviceType.File);
	rstDatabase.Devices.Add(bkpDevice);
	rstDatabase.ReplaceDatabase = true;
	rstDatabase.SqlRestore(sqlServer);

	return true;
}


Config:
HTML
<connectionStrings>
  <add name="adminSqlAccess" connectionString="Data Source=.\LOCAL2016;Initial Catalog=SystemManager2.0;Persist Security Info=True;User ID=sa;Password=xxxxxxxx" />
</connectionStrings>
<appSettings>
  <!-- ... -->
  <add key="dbPath" value="C:\Database\{0}\{1}"/>
  <add key="bakDbPath" value="C:\Database\blank\20170228-blank.bak"/>
</appSettings>
Posted
Updated 17-Mar-17 3:53am

The way I do it is here: Backing up an SQL Database in C#[^] - and I normally get the access violation message the other way round, when I try to save the backup. It's probably the same problem however - SQL doesn't run under your user ID, so the destination folder needs appropriate permissions for the restore.
 
Share this answer
 
Comments
Andy Lanng 17-Mar-17 6:45am    
Ha ha - my user ID is not MSSQL$LOCAL2016
I used Configure File System Permissions for Database Engine Access[^] which told me to set uo the instance user ID. I will try "NETWORK SERVICES" suggested my your link, but VS just crashed... again. 2015 seems to do that once or twice a day. Time for the 2017 move, me thinks.
OriginalGriff 17-Mar-17 7:07am    
You might want to check the lounge from yesterday before you upgrade VS...

I suspect it's the destination folder you create - that will be created under the user id of the application creating it, not under the SQL instance user - so it's quite likely that SQL doesn't have access to the folder and that why it fails to restore. Check the access permissions on the folder in Explorer and see who it says can do what.
Andy Lanng 17-Mar-17 7:10am    
Checked the new folder and child folders. Both MSSQL user and Network Services user propagated with full access rights :/
I'm gonna dig into the error and see if it'll tell me which user it's actually trying to use
Andy Lanng 17-Mar-17 7:26am    
Doesn't even work for "everyone" with full access
Andy Lanng 17-Mar-17 6:56am    
No Luck with Netwrok Servies user either
Oh for Pete's sake!

I omitted the filename from the relocate paths >_<

To be fair, "move" does imply moving from one folder to another, also the error message is identical to the permission issues most people seem to have hit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900