Search code examples
c#sql-serversqlconnectionquery-parser

Parse a SQL statement to see if it is anything other then a SELECT statement?


Is there a good way to parse a SQL statement to ensure it contains only a SELECT statement and nothing else? I am using C#, System.Data.SqlConnection, and MS SQL Server


Solution

  • You should connect to the database as a user that doesn't have permission to do anything other than a SELECT.

    This way, any non-SELECT statement will be unable to execute.

    This is the most secure solution possible, short of duplicating SQL Server's parser.