Simplified core issue below:
I have myscript.sql that has:
SELECT name FROM Sys.Databases GO USE mydatabase GO EXEC mystoredprocedure 'myparameter' GO
When I open cmd.exe and use:
SQLCMD -S localhost\myinstance -i script.sql
It executes fine.
When I open cmd.exe in C# using the Process class and execute the same command I get the following error:
name
-------------------
master
tempdb
model
msdb
mydatabase
(5 rows affected)
Msg 916, Level 14, State 1, Server localhost\myinstance, Line 1
The server principal "NT AUTHORITY\SYSTEM" is not able to access the database "mydatabase" under the current security context.
Msg 2812, Level 16, State 62, Server localhost\myinstance, Line 1
Could not find stored procedure 'mystoredprocedure'.
And now the detailed explaination:
I created an MSI which installs my custom application.
During the bootstrap process, SQL Server 2012 Express is installed using the following parameters:
/INSTANCEID="SQLEXPRESS"
/ACTION="Install"
/FEATURES=SQLEngine,Replication
/HELP="False"
/INDICATEPROGRESS="False"
/Q="True"
/QS="False"
/ROLE="AllFeatures_WithDefaults"
/ENU="True"
/ERRORREPORTING="False"
/SQMREPORTING=0
/INSTANCENAME="SQLEXPRESS"
/AGTSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE"
/AGTSVCSTARTUPTYPE="Disabled"
/ISSVCSTARTUPTYPE="Automatic"
/ISSVCACCOUNT="NT AUTHORITY\NetworkService"
/ASSVCSTARTUPTYPE="Automatic"
/ASCOLLATION="Latin1_General_CI_AS"
/ASDATADIR="Data"
/ASBACKUPDIR="Backup"
/ASTEMPDIR="Temp"
/ASCONFIGDIR="Config"
/ASPROVIDERMSOLAP="1"
/SQLSVCSTARTUPTYPE="Automatic"
/FILESTREAMLEVEL="0"
/ENABLERANU="True"
/SQLCOLLATION="SQL_Latin1_General_CP1_CI_AS"
/SQLSVCACCOUNT="NT Authority\Network Service"
/SECURITYMODE="SQL"
/ADDCURRENTUSERASSQLADMIN="True"
/RSSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE"
/RSSVCSTARTUPTYPE="Automatic"
/RSINSTALLMODE="FilesOnlyMode"
/HIDECONSOLE
/IACCEPTSQLSERVERLICENSETERMS
/SAPWD="***************"
The MSI then executes an Installer class DLL written in C# which restores a database to the SqlExpress instance.
When the restore is completed, the Installer class then uses the Process class to launch CMD.exe and execute the SQL script using SQLCMD.
Process vProcess = new Process(); ProcessStartInfo vStartInfo = new ProcessStartInfo("cmd.exe"); vStartInfo.Arguments = "/c set path=%path%;" + Context.Parameters["TargetDir"] + "\\; && sqlcmd -S LocalHost\\myinstance -i myscript.sql"; vProcess.StartInfo = vStartInfo; vProcess.Start(); vProcess.WaitForExit(30000);
This is where I get the error mentioned above.
However if I execute the same command manually by opening CMD.exe from the RUN command, it executes perfectly.
I can not use -U or -P to supply a user / password, I MUST use integrated security.
Additional info:
Previously SQL Server 2008 Express has been in use for the bootstrapper, and this issue did not occur.
The database the MSI restores is also built from SQL Server 2008. (Will be built from 2012 in the future.)
Installation is performed on an account with administrative rights.
Running the installer AS Administrator does not fix the issue.
Any help would be greatly appreciated, as well please let me know if additional info is required.
Thank you