471,355 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

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

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.
Jan 23 '06 #1
7 2478
David R. wrote:

<snip>
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

Jan 23 '06 #2
Hello Jon,

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

Thanks for your interest

David

"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:11*********************@g44g2000cwa.googlegro ups.com...
David R. wrote:

<snip>
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


Jan 23 '06 #3
David R. wrote:
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

Jan 24 '06 #4
David R. wrote:

<snip>

I've just tried the code you wrote, and it looks like the script is
executing to me.

If you could post a short but complete program that demonstrates the
problem, that would help a lot. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Jan 24 '06 #5
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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 24 '06 #6
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:11**********************@o13g2000cwo.googlegr oups.com...
David R. wrote:

<snip>

I've just tried the code you wrote, and it looks like the script is
executing to me.

If you could post a short but complete program that demonstrates the
problem, that would help a lot. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Jan 24 '06 #7
<"David R." <David R.@discussions.microsoft.com>> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 24 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by tukaram.thatikonda | last post: by
3 posts views Thread by deepak | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.