Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I was trying to do some kind of query optimization tree using C# .reading any query statement from user and then do some operations over it. but what i can't did was to check if this query is valid or not in other words i want to do something like Sqlsever parser to check query syntax without need to connect to SQLserver because this query is virtual query i have not any Tables or DataBases i just working in the query as it .


Note : I am using VS2005
please if any one can help .

Thanks.
Posted

If you have an SQL connection, you can avoid the need to write an SQL parser, by using the "SET NOEXEC ON" command before executing your SQL command. This tells SQL not to execute any commands, just evaluate if they would have been executed.
SqlCommand cmd = new SqlCommand("SET NOEXEC ON", conn);
cmd.ExecuteNonQuery();
cmd = new SqlCommand(sqlToBeChecked, conn);
cmd.ExecuteNonQuery();
If no exception is thrown, all is fine.
 
Share this answer
 
Yes, you need to write a query parser, which would check the rules of SQL to check if a query seems like it's validly formed. So you'd have to consider the various forms a SQL query can take, what words are reserved, etc, and write rules to parse the string. You can probably use regex to do at least a simple version of this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900