Determine whether a query returns results or not (before running it)

  • Thread starter Thread starter John Sway
  • Start date Start date
J

John Sway

I'm writing a web-based "Query analyser" tool for our company intranet. It
allows a user to type any SQL statement in a form, and execute it over the
Web.

The SQL can be a query that returns results (e.g: SELECT * FROM members) or
it can be a T-SQL (e.g: UPDATE/DELETE/INSERT).

What I want to know is, is there any way using either SQLCommand or DataSet
or any of the other ADO.NET classes to determine what kind of query it is,
BEFORE running it?

Then, if it is a T-SQL, I can just use the ExecuteNonQuery method, and I can
hide my datagrid that would contain the results. Or if it is a "SELECT"
query, I can populate a DataSet and show the results in a grid.

Do I have to manually parse out the SQL string to determine if it is SELECT
query? Or is there a more elegant way ?

Thanks!
 
All, SELECT, DELETE, UPdate, and Insert are SQL Statements (DML) and other
statements like Drop, Create, Alter are DDL. To determine if these (which I
think is what you want to do) being written, I'd use a Regex and just make a
determination on them. You could write a few different ways, but
essentially, you could make this determination pretty easy.

I have the regex's at work to do this if you are interested. Since you are
already going this far, you may want to test for a valid from, and if you
have a Group By statement, make sure you have an aggregate function listed.
Thanks to the kick a33 Regex engine .Net gives us, it's pretty
straightforward.

Cheers,

Bill
 
Back
Top