Geeks With Blogs
Wil Peck Experience is something you get right after you need it.

Ever need to parse SQL Syntax using .NET without actually executing the SQL?  Pretty sweet because you can actually do this with built in sql server options.  Works out pretty well.  Executing a statement with a syntax error, referencing an object that doesn’t exist, a stored procedure with parameters that has no parameters, etc produces a SqlException with an appropriate error message.

   1:  static void ParseQuery(string sql)
   2:  {
   3:      SqlConnection connection = new SqlConnection("some_connection_string");
   4:      SqlCommand command = new SqlCommand(
   5:          string.Format("SET NOEXEC ON {0} SET NOEXEC OFF", sql));
   6:      command.Connection = connection;
   7:      try
   8:      {
   9:          connection.Open();
  10:          command.ExecuteNonQuery();
  11:      }
  12:      finally
  13:      {
  14:          connection.Close();
  15:      }
  16:  }
Technorati Tags: ,,,
Posted on Thursday, August 5, 2010 11:34 AM | Back to top

Copyright © Wil Peck | Powered by: GeeksWithBlogs.net