2

I have a rich text field in my winform app where I can write sql script and need to check whether any issue is there in written script or not. Is there any way to do the same task just before it's execution using c#? Any type of suggestion is highly appreciated.

Thanks, Anil

6
  • can u elaborate ur question? what do u mean by parse sql script? Commented Jul 17, 2013 at 8:31
  • @Newton Sheikh, I have updated my requirement.Would you like to check again? Commented Jul 17, 2013 at 8:40
  • @Anil I still don't think it's enough. What do you want to check for? Commented Jul 17, 2013 at 8:42
  • You want to check whether your script is right or wrong before execution. Right? Commented Jul 17, 2013 at 8:45
  • i get ur question. U want to check if the sql query is valid or not before actually firing it on sql server or oracle or whatever. Well its quite tough. All u can do is check if the query syntax is valid or not(still its loads of coding). Try this codeproject.com/Articles/1294/SQL-Syntax-Validator Commented Jul 17, 2013 at 8:45

1 Answer 1

3

If you are only trying to validate the syntax (and not the existence of the database objects), you may take a look at the TSqlParser Class.

You will need to add the following references to your project:

  • Microsoft.Data.Schema.ScriptDom
  • Microsoft.Data.Schema.ScriptDom.Sql

A simple way to do that is to edit your .csproj file and add the following two entries along with the existing ones and reload the project:

<ItemGroup>
  <Reference Include="Microsoft.Data.Schema.ScriptDom.Sql" />
  <Reference Include="Microsoft.Data.Schema.ScriptDom" />
  <!-- other Reference tags -->
</ItemGroup>

Some sample usage:

private void btnParse_Click(object sender, EventArgs e)
{
    var parser = new TSql100Parser(true);
    IList<ParseError> errors;
    var result = parser.Parse(new StringReader(richTextBox1.Text), out errors);
    // TODO: check the errors
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.