Problem in executing a Batch-File created with C#-Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all,

I would like to generate a Sql-Script-File and a Batch-File to execute the
Batch-File over C#-Code and use the SQL-File as an Input-File for the command
"osql" in the Batch-File.
I generate the two files "attachDB.sql" and "setup.bat" with the following
code:

AttachAtpDBFile = File.CreateText(@"c:\attachDB.sql");
AttachAtpDBFile.AutoFlush = true;
AttachAtpDBFile.WriteLine("CREATE DATABASE {0}", DatabaseName);
AttachAtpDBFile.WriteLine("ON PRIMARY (FILENAME = '{0}')", AtpMdfFilePath);
AttachAtpDBFile.WriteLine("FOR ATTACH");
AttachAtpDBFile.WriteLine("go");
AttachAtpDBFile.WriteLine("USE ATP");
AttachAtpDBFile.WriteLine("go");
AttachAtpDBFile.Close();

SetupBatchFile = File.CreateText(@"c:\setup.bat");
SetupBatchFile.AutoFlush = true;
SetupBatchFile.WriteLine("@echo off");
SetupBatchFile.WriteLine("scm.exe -Action 1 -Silent 1 -Service MSSQLServer");
SetupBatchFile.WriteLine(@"osql -E -i {0} > c:\log.txt", @"c:\attachDB.sql");
SetupBatchFile.WriteLine("@echo on");
SetupBatchFile.Close();

I execute the Batch-File with the following code:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName= @"c:\setup.bat";
proc.StartInfo.Arguments="";
proc.Start();
proc.WaitForExit();

The problem is that when I'm executing the Batch-File the command "osql"
returns following error message:

The Inputfile attachDB.sql can not be opened. File or directory is not
available.

But when I'm executing the Batchfile over a shell or an explorer the script
creates the database successfully (in this case the file attachDB.sql is
available)!

Does someone know what I'm doing wrong, or what I have to consider?

With kind regards

David R.
 
David R. wrote:

Does someone know what I'm doing wrong, or what I have to consider?

Was that *exactly* your code, or did you change some of it for the
purposes of posting it? For instance, that wrote to c:\, but if you
were *actually* writing to a file in a directory with spaces in it, you
might need to account for that in the batch file.

If that's your exact code, I'll give it a try in the morning and see
what happens...

Jon
 
Hello Jon,

this was not the exactly path, but the real path doesn't have any spaces in
it!

Thanks for your interest

David
 
David said:
this was not the exactly path, but the real path doesn't have any spaces in
it!

Hmm. Could you confirm that the code you *did* post doesn't work? It
would be good to be able to reproduce a situation which genuinely
doesn't work for you.

Jon
 
Hi,


The Inputfile attachDB.sql can not be opened. File or directory is not
available.

But when I'm executing the Batchfile over a shell or an explorer the
script
creates the database successfully (in this case the file attachDB.sql is
available)!

Does someone know what I'm doing wrong, or what I have to consider?

You should use FQN for the files, also I think you better use
Path.GetTempFileName for both query and bat file.

maybe your problem is how you concatenate the files to create the path, I
would double check that part
 
Hello Jon,

I have found the error!
The error accrued in the following row:

SetupBatchFile.WriteLine(@"osql -E -i {0} > c:\log.txt",
@"c:\attachDB.sql");

here in the newsgroup I wrote the path "c:\attachDB.sql" in plain text but
in the application I get it by a variable.
The error was that the value from the variable was not "c:\attachDB.sql" but
only "attachDB.sql"!

Thank you for your support!

With kind regards from Germany

David
 
I have found the error!
The error accrued in the following row:

SetupBatchFile.WriteLine(@"osql -E -i {0} > c:\log.txt",
@"c:\attachDB.sql");

here in the newsgroup I wrote the path "c:\attachDB.sql" in plain text but
in the application I get it by a variable.
The error was that the value from the variable was not "c:\attachDB.sql" but
only "attachDB.sql"!

I suspected that might be the case. Working out a short but complete
example to post is often a good way of finding an error without ever
having to even post anything :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top