Reputation: 48558
I have a SelectSQLQueryBuilder in which there is a read only text box. the user inputs data and the string is generated!! i have to check whether this string is an SQL statement.
In SQL SERVER MGMT STUDIO, there is a button right next to execute that does that without firing the statement. Any Class in .NET that does so? How can i validate this string !! please help!!
Upvotes: 1
Views: 1462
Reputation: 1892
If you send the statement to SQLServer like this
set PARSEONLY ON
your statement
the server will parse your statement but not execute it and return any errors. If it passes you will know it's a valid statement.
You will not know if it would actually run because some kinds of errors are not detected by PARSEONLY. Those are, for example, errors where your SQL is valid but it causes illegal operations such as data conversions or constraint violations.
So while there may not be a class in .Net, you can make parseonly happen by adding the set command to your sql. I believe this is the feature used by SQL Management Studio. You might notice that SQL Mgt. Studio cannot parse a statement unless it has a server connection.
PARSEONLY is a connection level setting. So if you turn it on, you need to turn it off again if you actually want to execute SQL on that connection.
Upvotes: 2
Reputation: 15683
For Transact-SQL, you can look into using TSql100Parser Class.
Also there is a product called General SQL Parser, it's not free but has been around for awhile.
Upvotes: 1
Reputation: 27419
The problem, generally, is parsing the SQL statement. It turns out that this is fairly difficult becuase of the complexities of the language as well as the numerous variations.
A good library exists which handles these complexities and parses SQL- http://sqlparser.com/.
Upvotes: 0