Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running script with sqlcmd 1

Status
Not open for further replies.

CaptainD

Programmer
Jul 13, 1999
644
US
Im trying to run a T-Sql script using Sqlcmd.exe through a C# button click event. I can run the script through the command prompt but it's not running through C#
Note: Through C# a command window pops up for a short time like it runs but the output text file is not created.
Through the command line, the output text file is created with the data requested by the script.

Code:
 using System.Diagnostics;

Code:
        private void button1_Click(object sender, EventArgs e)
        {          
           Process RunSQLScript = new Process();
           RunSQLScript.StartInfo.FileName = "sqlcmd.exe";
           RunSQLScript.StartInfo.Arguments = "- E -S clovis-003 -i c:\test.sql -o c:\testData.txt";
           RunSQLScript.Start();

        }
 
1) In the line where you specify the arguments, there shouldn't be a space between '-' and 'E'. That might cause an error on the commandline.

2) I once wrote a GUI to make T-SQL easier for me, and I redirected the output from the commandline back to the program to control everything and intercept errors. I used the RedirectLib ( to accomplish this. Take a look at the page and examples, I think you can use it also.

Regards, Ruffnekk
---
Basic Instructions Before Leaving Earth (B.I.B.L.E.)
 
I figured it out after some research. For some reason (I have not found out why yet) C# Requires two \\ for the drive "C:\\" instead of "C:\" here is the code that works
Code:
        private void button1_Click(object sender, EventArgs e)
      
        {
            try
            {
           Process RunSQLScript = new Process();
           RunSQLScript.StartInfo.FileName = "sqlcmd";
           RunSQLScript.StartInfo.Arguments = "-E -S clovis-003 -i c:\\test.sql -o c:\\testData.txt";
           RunSQLScript.Start();
            }
           catch(Exception f)
            {
                Console.WriteLine("An error occurred: '{0}'", f);
            }
    
        }

Thanks for the help
 
C# requires a second \ because the \ is an escape character. For instance, if you had a string like the following:

Code:
string myString = "When Chris said, "Eviscerate the Proletariat", I almost peed my pants";

It would treat that as a couple of strings because of the quotes. You would have

"When Chris said, "

and

", I almost peed my pants"

Eviscerate the Proletariat would not be part of the string and would actually cause an error. To get around this problem, you escape the inner quotes, like so:

Code:
string myString = "When Chris said, \"Eviscerate the Proletariat\", I almost peed my pants";

This would give the desired effect. So every time C# comes upon a \ it thinks to escape the next character afterward.

What if you WANTED a literal \ in the string? You merely escape the escape character like so:

Code:
string myFile = "c:\\Test\\Test.txt";

Hopefully, that will help explain it a bit.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
I did not know that, thanks for the info and the great explanation!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top