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<string> 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<string> 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);
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dDsKdXNpbmcgU3lzdGVtLkxpbnE7CnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOwp1c2luZyBTeXN0ZW0uUmVmbGVjdGlvbjsKICAgICAgICAKcHVibGljIGNsYXNzIFByb2dyYW0KewpwdWJsaWMgc3RhdGljIHZvaWQgTWFpbihzdHJpbmdbXSBhcmdzKQp7CiAgICBzdHJpbmcgaW5zZXJ0U3RyaW5nID0gQCJJTlNFUlQgSU5UTyB0YjEoZmllbGQxLGZpZWxkMixmaWVsZDMpIFZBTFVFUyAoMSwyLDMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIElOU0VSVCBJTlRPIHRiMShmaWVsZDEsZmllbGQyLGZpZWxkMykgVkFMVUVTICg0LDUsNik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgSU5TRVJUIElO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