I have a small program that let students test their SQL queries built in ASP.Net and using an SQL Server Database. The problem is that some queries "hang" the console and SQL Server. With that in mind, I want to limit the execution time to 10s or throw an exception.
Right now the code looks something like this:
SqlCommand cmd = new SqlCommand();
cmd.CommandText = TextBox1.Text; //Their query
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 1;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
try
{
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
int count = GridView1.Rows.Count;
Label1.Text = count.ToString() + " Rows";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
GridView1.DataSource = null;
GridView1.DataBind();
}
finally
{
sqlConnection1.Close();
}
CommandtTimeout
is not doing it for me because often times the problem is that the queries are continually streaming rows (Mainly due to a cartesian product operation).
Is there a way to stop the query mid execution without looping through every row?