class ConsoleApp
{
static void Main(string[] args)
{
// Create a new instance of cancelTest class.
// Note that the query will return 10,000 rows.
cancelTest test =
new cancelTest("Data Source=Teradata1;User ID=ab;Password=ab;",
"Select StatementText From AccLogTbl sample 10000");
// Start the test.
test.Start();
}
}
class cancelTest
{
private TdConnection cn;
private TdCommand cmd;
public cancelTest(String connectionString, String commandText)
{
// Initialize the connection object
cn = new TdConnection(connectionString);
// Initialize the command object.
cmd = new TdCommand(commandText, cn);
}
public void Start()
{
Thread executeThread = new Thread(new ThreadStart(this.ExecuteQuery));
Thread cancelThread = new Thread(new ThreadStart(this.CancelQuery));
executeThread.Start();
cancelThread.Start();
executeThread.Join();
cancelThread.Join();
}
public void ExecuteQuery()
{
try
{
// open a session to Teradata
cn.Open();
// Execute the query to retrieve 10,000 rows.
TdDataReader reader = cmd.ExecuteReader();
// write 10,000 rows to console
int recordNumber = 1;
while(reader.Read())
{
Console.WriteLine("row[{0}], column[1] = {1}",
recordNumber++, reader.GetString(0));
}
// Close the reader
reader.Close();
}
catch(TdException e)
{
Console.WriteLine(e.Message);
}
catch(SystemException e)
{
Console.WriteLine(e.Message);
}
finally
{
// close the session to Teradata
cn.Close();
}
}
public void CancelQuery()
{
try
{
// wait a while and let the other thread open
// the session and execute the query
Thread.Sleep(30000);
// Cancel the command
cmd.Cancel();
}
catch(System.InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
}
}