0

I am having trouble attaching a SQL Server 2008 R2 Express database. I have to do it via a .Net exe.

Here is my code

string fileName = @"C:\express\createdb.sql";
ProcessStartInfo psiSqlCmd = new ProcessStartInfo("sqlcmd.exe", @" -S            .\SQLExpress -U sa -o c:\express.out\txt -i """ + @fileName + @""" -P sa123");

psiSqlCmd.UseShellExecute = false;
psiSqlCmd.CreateNoWindow = true;
psiSqlCmd.WindowStyle = ProcessWindowStyle.Hidden;
psiSqlCmd.Verb = "runas";
Process sqlCmdProcess = new Process();

sqlCmdProcess.StartInfo = psiSqlCmd;
sqlCmdProcess.Start();   

Here is the code in createdb.sql:

USE master;
GO
CREATE DATABASE FishLite 
ON (FILENAME = 'C:\Express\FishLite.mdf')
FOR ATTACH;
GO

I get an access denied error on FishLite.mdf file. If I run command prompt under administrative rights and run the createdb.sql it works fine. But I can't make it work when I run it via c# code.

I am launching the process under administrative rights but still its not working.

JustinStolle
  • 4,182
  • 3
  • 37
  • 48
adil
  • 101
  • 1
  • 7

0 Answers0