This project is read-only.

Problem attaching database using SqlCmd

Oct 24, 2013 at 11:51 PM
Edited Oct 24, 2013 at 11:52 PM
I have been trying to use the SqlCmd class to attach a database and for some reason, I am constantly encountering this error message:

MSBUILD : warning 5120: Unable to open the physical file "C:\Database.mdf". Operating system error 5: "5(Access is denied.)". [C:\DevSolutionBuild.proj]

This is the MSBuild command that I am using:
      <UseMasterDb>USE [master]</UseMasterDb>
      <DNNAttachSQL>CREATE DATABASE $(DNNDatabaseName) ON ( FILENAME = N'$(DNNDataFile)' ),( FILENAME = N'$(DNNLogFile)' ) FOR ATTACH;</DNNAttachSQL>
    <Message Text="$(DNNAttachSQL)" Importance="high" />
    <MSBuild.ExtensionPack.SqlServer.SqlCmd TaskAction="Execute" Server="$(SqlServerName)" LogOn="$(SqlLogOn)" Password="$(SqlPassword)" CommandLineQuery="$(DNNAttachSQL)" />
The SQL executes just fine when I run it directly in SQL Server Management Studio and it succeeds in attaching the database, however, when I run it from within MSBuild, it always fails.

Please advise as to what I might be doing wrong.

Nov 29, 2013 at 9:05 AM
Seems like a UAC error. What identitiy is executing the MSBuild? Does it work of if you dont create it in C: (protected) and rather something like C:\Test

Marked as answer by mikeFourie on 1/14/2014 at 6:50 AM