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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David R.

    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.s ql" and "setup.bat" with the following
    code:

    AttachAtpDBFile = File.CreateText (@"c:\attachDB. sql");
    AttachAtpDBFile .AutoFlush = true;
    AttachAtpDBFile .WriteLine("CRE ATE 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("@ech o off");
    SetupBatchFile. WriteLine("scm. exe -Action 1 -Silent 1 -Service MSSQLServer");
    SetupBatchFile. WriteLine(@"osq l -E -i {0} > c:\log.txt", @"c:\attachDB.s ql");
    SetupBatchFile. WriteLine("@ech o on");
    SetupBatchFile. Close();

    I execute the Batch-File with the following code:

    System.Diagnost ics.Process proc = new System.Diagnost ics.Process();
    proc.EnableRais ingEvents=false ;
    proc.StartInfo. FileName= @"c:\setup.bat" ;
    proc.StartInfo. Arguments="";
    proc.Start();
    proc.WaitForExi t();

    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.
  • Jon Skeet [C# MVP]

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

    David R. wrote:

    <snip>
    [color=blue]
    > Does someone know what I'm doing wrong, or what I have to consider?[/color]

    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

    Comment

    • David R.

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

      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]" <skeet@pobox.co m> schrieb im Newsbeitrag
      news:1138057171 .942918.11620@g 44g2000cwa.goog legroups.com...[color=blue]
      > David R. wrote:
      >
      > <snip>
      >[color=green]
      >> Does someone know what I'm doing wrong, or what I have to consider?[/color]
      >
      > 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
      >
      >[/color]



      Comment

      • Jon Skeet [C# MVP]

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

        David R. wrote:[color=blue]
        > this was not the exactly path, but the real path doesn't have any spaces in
        > it![/color]

        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

        Comment

        • Jon Skeet [C# MVP]

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

          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

          Comment

          • Ignacio Machin \( .NET/ C# MVP \)

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

            Hi,


            [color=blue]
            > 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?[/color]

            You should use FQN for the files, also I think you better use
            Path.GetTempFil eName 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


            Comment

            • David R.

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

              Hello Jon,

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

              SetupBatchFile. WriteLine(@"osq l -E -i {0} > c:\log.txt",
              @"c:\attachDB.s ql");

              here in the newsgroup I wrote the path "c:\attachDB.sq l" 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.sq l" but
              only "attachDB.s ql"!

              Thank you for your support!

              With kind regards from Germany

              David

              "Jon Skeet [C# MVP]" <skeet@pobox.co m> schrieb im Newsbeitrag
              news:1138089476 .588231.270310@ o13g2000cwo.goo glegroups.com.. .[color=blue]
              > 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
              >[/color]


              Comment

              • Jon Skeet [C# MVP]

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

                <"David R." <David R.@discussions. microsoft.com>> wrote:[color=blue]
                > I have found the error!
                > The error accrued in the following row:
                >
                > SetupBatchFile. WriteLine(@"osq l -E -i {0} > c:\log.txt",
                > @"c:\attachDB.s ql");
                >
                > here in the newsgroup I wrote the path "c:\attachDB.sq l" 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.sq l" but
                > only "attachDB.s ql"![/color]

                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 - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                Working...