01 February 2011

.NET/SQL - Split multiple statements

For the project 'DatabaseTool', I had to split a query that could contain multiple statements into those multiple statements. For this I required each statement to end with a ';' and start on a new line. I wrote the following function to split them:

public IEnumerable GetStatements(string query) {
  string[] parts = query.Split(new string[] { ";\r\n" },
                               StringSplitOptions.RemoveEmptyEntries);
  return from p in parts
         select p.Trim(new char[] { ' ', ';', '\t' });
}

I didn't split on white lines because those can also occur within statements; and it's to risky when UPDATE's and WHERE's are involved.

No comments:

Post a Comment