using System; using System.Text; using System.Linq; using System.Collections.Generic; using System.Reflection; public class Program { public static void Main(string[] args) { string insertString = @"INSERT INTO tb1(field1,field2,field3) VALUES (1,2,3); INSERT INTO tb1(field1,field2,field3) VALUES (4,5,6); INSERT INTO tb1(field1,field2,field3) VALUES (7,8,9);"; IEnumerable inserts = insertString.Split(new[] { Environment.NewLine }, StringSplitOptions.None); string firstInsert = inserts.First(); int tableIndex = firstInsert.IndexOf("INSERT INTO ") + "INSERT INTO ".Length; string table = firstInsert.Substring( tableIndex, firstInsert.IndexOf("(", tableIndex) - tableIndex); var regex = new System.Text.RegularExpressions.Regex(@"\(([^)]+)\)", System.Text.RegularExpressions.RegexOptions.Compiled); string columns = regex.Matches(firstInsert)[0].Value; IEnumerable values = inserts.Select(sql => regex.Matches(sql)[1].Value); string insertAll = string.Format("INSERT INTO {0}{1} VALUES {2};" , table , columns , string.Join(",", values.ToArray())); Console.Write("Result: " + insertAll); } }