使用MS的ScriptDom来拆解TSQL脚本

此处提供9.1.40413.0版本的DLL一共4个:Microsoft.Data.Schema.dll、Microsoft.Data.Schema.ScriptDom.dll、Microsoft.Data.Schema.ScriptDom.Sql.dll、Microsoft.Data.Schema.Sql.dll。传送:https://pan.baidu.com/s/1geOH7qz

废话不多说,直接上代码:

public static IEnumerable<string> ParserInternal(TextReader reader) { var parser = new TSql100Parser(false); var errors = new List<ParseError>(); var tokens = parser.GetTokenStream(reader, errors); if (errors.Count > 0) { var sbError = new StringBuilder(); sbError.AppendLine("SQL Script Parse Failed!"); foreach (var error in errors) { sbError.AppendLine($"\tLine:{error.Line} Column:{error.Column} Error:{error.Message}"); } throw new InvalidDataException(sbError.ToString()); } var isSkip = true; var sbScript = new StringBuilder(); foreach (var token in tokens) { switch (token.TokenType) { case TSqlTokenType.SingleLineComment: case TSqlTokenType.MultilineComment: case TSqlTokenType.WhiteSpace: sbScript.Append(token.Text); break; case TSqlTokenType.Go: if (!isSkip) { isSkip = true; yield return sbScript.ToString(); } sbScript = new StringBuilder(); break; default: isSkip = false; sbScript.Append(token.Text); break; } } if (sbScript.Length > 0) yield return sbScript.ToString(); }

猜你喜欢

转载自www.cnblogs.com/xuhaibiao/p/9251502.html
ms