Calling BTEQ script from a .NET Application

Connectivity

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

2 REPLIES
Teradata Employee

Re: Calling BTEQ script from a .NET Application

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
Enthusiast

Re: Calling BTEQ script from a .NET Application

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);
}

}