Ejemplo n.º 1
0
        public static DataTable NewDataTableFromInsertScript(string insertScriptPath, string dataTableXml)
        {
            DataTable dataTable = DataParser.NewDataTableFromXml(dataTableXml);

            FileInfo insertScriptFileInfo = new FileInfo(insertScriptPath);

            if (insertScriptFileInfo.Exists)
            {
                string insertScript = File.ReadAllText(insertScriptFileInfo.FullName);
                // Break up insert script into parts
                Match regexMatch = Regex.Match(insertScript, @"INSERT INTO \[(?<TableSchema>[^\]\[]{1,})\].\[(?<TableName>[^\]\[]{1,})\]\s*\((?<ColumnNames>[^\)\(]{1,})\)\s*VALUES\s*(?<Rows>\((?s:.*)\))");
                if (regexMatch.Success)
                {
                    string   tableSchema = string.Empty;
                    string   tableName   = string.Empty;
                    string[] columnNames = new string[0];
                    string   rows        = string.Empty;

                    Group matchSearch = regexMatch.Groups["TableSchema"];
                    if (matchSearch.Success)
                    {
                        tableSchema = matchSearch.Value;
                    }
                    matchSearch = regexMatch.Groups["TableName"];
                    if (matchSearch.Success)
                    {
                        tableName = matchSearch.Value;
                    }
                    matchSearch = regexMatch.Groups["ColumnNames"];
                    MatchCollection regexMatches;
                    if (matchSearch.Success)
                    {
                        regexMatches = Regex.Matches(matchSearch.Value, @"\[(?<ColumnName>[^\]\[]+)\]");
                        columnNames  = new string[regexMatches.Count];
                        for (int i = 0; i < regexMatches.Count; i++)
                        {
                            if (regexMatches[i].Success)
                            {
                                columnNames[i] = regexMatches[i].Groups["ColumnName"].Value;
                            }
                        }
                    }
                    matchSearch = regexMatch.Groups["Rows"];
                    if (matchSearch.Success)
                    {
                        rows = matchSearch.Value;
                    }

                    regexMatches = Regex.Matches(rows, @"\((?<RowValues>.*)\),?");
                    foreach (Match match in regexMatches)
                    {
                        if (match.Success)
                        {
                            matchSearch = match.Groups["RowValues"];
                            if (matchSearch.Success)
                            {
                                DataRow  newRow      = dataTable.NewRow();
                                string[] rowValues   = new string[columnNames.Count()];
                                int      columnIndex = 0;
                                foreach (TSQLToken tSqlToken in TSQLTokenizer.ParseTokens(matchSearch.Value))
                                {
                                    if (!(tSqlToken is TSQLCharacter && tSqlToken.Text == ","))
                                    {
                                        newRow[columnNames[columnIndex]] = SqlValueStringToTypedObject(
                                            tSqlToken.Text,
                                            dataTable.Columns[columnNames[columnIndex]].DataType);
                                        columnIndex++;
                                    }
                                }
                                dataTable.Rows.Add(newRow);
                            }
                        }
                    }
                }
            }

            return(dataTable);
        }