Query Validation using c#

validate sql query without executing c#
tsql100parser
how to validate sql query results
how to validate sql query in java
c# query parser
sql parser c#
how to check if sql query is correct
validate sql syntax before execution

I am looking for a query validator in c#, which allows me to parse the SQL Text from textbox and verify wether its correct or not before sending it for execution. (MS SQL or DB2 queries).

If you want to validate SQL syntax without the use of a database, the TSql100Parser class will do well for this situation.

Disclaimer, code borrowed from this post here Code to validate SQL Scripts

Pretty straightforward to use though. If it returns null, then there were no errors in parsing it.

using Microsoft.Data.Schema.ScriptDom;
using Microsoft.Data.Schema.ScriptDom.Sql;

public class SqlParser
{
        public List<string> Parse(string sql)
        {
            TSql100Parser parser = new TSql100Parser(false);
            IScriptFragment fragment;
            IList<ParseError> errors;
            fragment = parser.Parse(new StringReader(sql), out errors);
            if (errors != null && errors.Count > 0)
            {
                List<string> errorList = new List<string>();
                foreach (var error in errors)
                {
                    errorList.Add(error.Message);
                }
                return errorList;
            }
            return null;
        }
}

[Solved] How to check SQLquery syntax is valid or not using c# , 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. c# - Validate SQL Query with Regular Expression - Stack Overflow. I want to validate a string that contains following words: SELECT and FROM but do not contain a group of words like CREATE, DROP, UPDATE etc. To be more specific, i want to ensure that a user will. Stack Overflow. Products.

Set your query to sql with this hint:

set PARSEONLY  on

It just checks your query and returns, like this:

set PARSEONLY  on
select * from tablea

Returns no exception.

While

set PARSEONLY  on
select * f rom tablea

returns

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'f'.

How to check SQLquery syntax is valid or not using c# ?, 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. The only case you are not handling in the above code is when no 'num' is passed in the query string. I'm not sure what you want to do in this case, but you could remove the outer if block, so that your exception is thrown if the parameter is not passed at all.

If you would like to validate/parse just a SELECT statement, regardless of how "heavy-duty" that select statement is, I found out that the best and fastest way to validate a select statement is the following: - in your code create 2 select statements (strings) such as:

1) Your valid select statement: SELECT * FROM HUGE_TABLE JOIN MULTIPLE_TABLES WHERE <<Condition>> 2) Create a similar select statement such as SELECT TOP 1 * FROM HUGE_TABLE JOIN MULTIPLE_TABLES WHERE <<Condition>> - Parse/Validate just the second one, regardless of how many joins you have in there, it will parse it in milliseconds, such as:

SqlCommand sqlParse = new SqlCommand(ParseSelectStatement, sqlConn); 

try 
{
sqlConn.Open();
sqlParse.ExecuteNonQuery()
}

Hope it helps! Cheers!

RequestValidator Class (System.Web.Util), The following example shows how to create a custom request validator that allows only a specific string to be used for query-string values. C# Copy. using System;� Is it possible to validate query parameters on an action without using a model? A lot of the calls in my API are one-offs and I don't see a point in making models for them if they will only be used one time.

I think this is what you are looking for. http://www.codeproject.com/KB/database/sqlvalidator.aspx

Custom SQL Query Validation | Reporting, The following code illustrates how to implement the validator and the service: C#; VB.NET. using DevExpress.DataAccess.ConnectionParameters� Query Validation using c#. 0. SQL migration tool. 1. Validate SQL queries and result sets. 0. Code to Validate SQL SELECT Statement Script-1. Powershell script to

C# Check if SQL query is syntactically correct , I am using Visual Studio 2010 (Framework 3.5) ArcMap 10.1. I am developing a project in C# and am having trouble developing a method to� Query Validation using c#. I am looking for a query validator in c#, which allows me to parse the SQL Text from textbox and verify wether its correct or not before sending it for execution. (MS SQL or DB2 queries). MS SQL - is the best query validator.

Using F# to verify sql queries in C# projects at build time, There might be other ways to validate sql queries at build time but I am not aware of those, please let me know if there are other way of validating� "File" - "New Website" - "C# - Empty website" (to avoid adding a master page) Give the web site a name, such as Validation or whatever you wish and specify the location; Then right-click on the solution in the Solution Explorer then select "Add New Item" - "Default.aspx page" (add two pages).

How to validate an SQL query? (SQL syntax checking and validity , SQL Syntax correctness validation. Active Query Builder can check SELECT queries for syntax correctness against dozens of database servers. This article discusses client-side validation using jQuery validation. That is, we will use JavaScript to validate the fields before submitting the form to the server. This is fast and efficient and provides quick replies to your visitor in the event of any errors. However, it is advisable to also validate the data on the server-side before

Comments
  • MS SQL - is the best query validator. Send the query to it and it will throw exception is the query is wrong. :) Also, the same query can be correct for one DB engine and incorrect for the other one. Why not to let DB engine validate?
  • ExecuteSQL on MS SQL which would throw a Exception on invalid query ,but potential harm lay wherein these Queries would be fired where as my only intention is to test the Syntax. And what in case of DB2 ?
  • what overhead? DB query validation is one of the very first steps in the query workflow. It will not just validate the syntax, but also check if table or columns exist and all kinds of things like this.
  • Am I missing something ? I am not able to get references for Microsoft.Data. I am using VS2010
  • @Kunal You have to right click on references and add this reference to your project before you can do a using statement to include it.
  • well I have added both references Microsoft.Data.Schema.ScriptDom and Microsoft.Data.Schema.ScriptDom.Sql, but in using statement it shows error on "Data"and hence not able to get TSql100Parser class.
  • Any such class to validate DB2 Queries ?
  • TSql100Parser is part of Microsoft.SqlServer.TransactSql.ScriptDom.dll IScriptFragment with newer VSBuilds is now TSqlFragment
  • Well. But this one will make a DB call, parse it on Client end and validate SQL text. I want to avoid DB Calls. Also, any such option for DB2 SQLs ?
  • this requires you to run the query if its successful. OP is asking for to "parse the SQL Text from textbox and verify wether its correct or not before sending it for execution" - I understand the next logical step IS to run it but your method is not what OP is asking
  • This is not easily usable from c#
  • Cool. But is there anything useful for DB2 Queries ?