All Forums Connectivity
arunsankar 2 posts Joined 05/08
14 May 2008
Calling BTEQ script from a .NET Application

Hi all,Is there a way to call a BTEQ script from a .NET application and if so is it possible to pass parameters to the BTEQ script in any way....Thanks in advance for the inputs...Thanks,Arun

Adeel Chaudhry 773 posts Joined 04/08
21 May 2008

Hello,Have a look at "System.Diagnostics.Process" class in .NET. Using that you can run BTEQ and pass arguments to it, or you can directly run "BTEQ < scriptname.btq".Regards,Adeel

-- If you are stuck at something .... consider it an opportunity to think anew.

rluebke 65 posts Joined 11/05
16 Jul 2008

Arun, Below is a pair of classes that I use to run BTEQ from C#. It's pretty simple really. You create a BTEQJob object and set the LogFileName and the SQLFileName properties and then call the Start Method. It is a bit more complicated than using redirection from the operating system, but you have more control.R===========================================================using System.Diagnostics;using System.IO; /*This is just used to log the output from the BTEQ Process */ static class JobLog { static object locker = new object(); static public void Writer(String LogFile, String Message) { lock (locker) using (StreamWriter sw = new StreamWriter(LogFile, true)) { sw.WriteLine(String.Format("{0:G}\t{1}", DateTime.Now, Message)); sw.Flush(); sw.Close(); } } } class BTEQJob { private String BTEQ = @"C:\Program Files\NCR\Teradata Client\bin\bteq.exe"; public String LogFileName = String.Empty; public String SQLFileName = String.Empty; private Process BTEQProcess; public void Start() { File.Delete(LogFileName); BTEQProcess = new Process(); BTEQProcess.StartInfo.FileName = BTEQ; BTEQProcess.StartInfo.CreateNoWindow = true; /* Redirect StdIO */ BTEQProcess.StartInfo.UseShellExecute = false; BTEQProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(SQLFileName); BTEQProcess.StartInfo.RedirectStandardInput = true; BTEQProcess.StartInfo.RedirectStandardOutput = true; BTEQProcess.StartInfo.RedirectStandardError = true; /* Handle process events */ /* I log STDERR and STDOUT to the same log file */ BTEQProcess.EnableRaisingEvents = true; BTEQProcess.OutputDataReceived += new DataReceivedEventHandler(BTEQProcess_OutputDataReceived); BTEQProcess.ErrorDataReceived += new DataReceivedEventHandler(BTEQProcess_OutputDataReceived); BTEQProcess.Exited += new EventHandler(BTEQProcess_Exited); /* Start This Puppy */ BTEQProcess.Start(); BTEQProcess.BeginOutputReadLine(); BTEQProcess.BeginErrorReadLine(); using (StreamReader sr = new StreamReader(SQLFileName)) { BTEQProcess.StandardInput.WriteLine(sr.ReadToEnd()); sr.Close(); } } private void BTEQProcess_Exited(object sender, EventArgs e) { //Console.WriteLine(Program.BTEQCnt.ToString()); if (BTEQProcess.ExitCode > 0) { Program.CurrentExitCode = BTEQProcess.ExitCode; Program.FailedScript = Path.GetFileName(SQLFileName); } Program.BTEQCnt--; } private void BTEQProcess_OutputDataReceived(object sendingProcess, DataReceivedEventArgs outLine) { if (!String.IsNullOrEmpty(outLine.Data)) JobLog.Writer(LogFileName, outLine.Data); } }

You must sign in to leave a comment.