This can be done with a regular expression, but it might not be the best way if you have many different SQL statements you want to parse.
This solution only works for the insert statement.
private static Regex insertExpr = new Regex(@"INSERT INTO\s*\[\w+\].\[\w+\]\s*\((?<columns>[\S\s]+?)\)\s* VALUES\s*\((?<values>[\S\s]+?)\)", RegexOptions.IgnoreCase);
private static Regex contentExpr = new Regex(@"(\[)?(?<val>[\d\w]+)(\])?", RegexOptions.IgnoreCase);
string input = @"INSERT INTO [dbo].[t]
([c1]
,[c2]
,[c3])
VALUES
(123,
'abc',
321)";
Match m = insertExpr.Match(input);
if (m.Success)
{
MatchCollection cols = contentExpr.Matches(m.Groups["columns"].Value);
MatchCollection values = contentExpr.Matches(m.Groups["values"].Value);
if (cols.Count != values.Count)
throw new Exception("Something fishy is going on.");
for (int i=0; i< cols.Count; i++)
{
Debug.WriteLine("{0} = {1}", cols[i].Groups["val"].Value, values[i].Groups["val"].Value);
}
}
The exact output you can modify to your liking.
The other option is to use a lexer and parser, but that is a bit more complicated to get started with.
MinosseCC: a lexer/parser generator for C#[
^]
A New Parser Generator for C#[
^]