473,372 Members | 947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 2563
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: danny | last post by:
Howdy, I've run into a problem pickling objects created with a type statement. Here's the code: ********************************************* import pickle class foo(object): def...
0
by: new1s.on.sympatico.ca | last post by:
Hey There! Any one have any ideas on how to prevent the DOS window from appearing when executing an Output Assembly (exe) generated using CodeDom? Thanks, Roland
0
by: tukaram.thatikonda | last post by:
Hi Guys, I have written a small windows application in VB.Net to test ADO.Net performance while executing long running query. The query works most of the time but fails sometime. I am trying...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
1
by: Klaus Jensen | last post by:
Hi! This has been annoying me all day, and I can't get it to work It is really driving me nuts! Basicly this simple webapp created to illustrate my problem, renders five buttons, and adds a...
20
by: Neil Robbins | last post by:
I am trying to execute a make table query in my vb.net program. The db that I am accessing is an Access 2000 format db. The SQL code that I am using has been cut and pasted from the SQL View having...
0
by: blueblueblue | last post by:
Hi, I am trying to execute my dts package from sql using command shell as exec master..xp_cmdshell 'dtsrun /Sfiutopiadb /Usa /P /NBulktest' The package works perfectly in enterprise...
10
by: olafilink | last post by:
Hello, we're running an Win2003server with IIS6 and PHP4 in fastCGI config. When i try to use system() command to execute ffmpeg.exe it works ok when i run the PHP script from the commandline on...
3
by: deepak | last post by:
Hi There, i am facing problems while executing the query using ADO.NET,the problem goes below: i am using ExecuteDataset(sqlTran, CommandType.Text, query) and my query is this SELECT * FROM...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.