如何在不使用ActiveQueryBuilder的情况下解析SQL语句(对于SQL Server)以提取列和参数信息(Name,DataType)。
此致
答案 0 :(得分:2)
您可以尝试使用TSql100Parser class
链接:http://msdn.microsoft.com/fr-fr/library/microsoft.data.schema.scriptdom.sql.tsql100parser.aspx
示例:
bool fQuotedIdenfifiers = false;
var _parser = new TSql100Parser(fQuotedIdenfifiers);
SqlScriptGeneratorOptions options = new SqlScriptGeneratorOptions();
options.SqlVersion = SqlVersion.Sql100;
options.KeywordCasing = KeywordCasing.UpperCase;
_scriptGen = new Sql100ScriptGenerator(options);
IScriptFragment fragment;
IList<ParseError> errors;
using (StringReader sr = new StringReader(inputScript))
{
fragment = _parser.Parse(sr, out errors);
}
if (errors != null && errors.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (var error in errors)
{
sb.AppendLine(error.Message);
sb.AppendLine("offset " + error.Offset.ToString());
}
var errorsList = sb.ToString();
}
else
{
String script;
_scriptGen.GenerateScript(fragment, out script);
var result = script;
}
答案 1 :(得分:1)
好吧,解析语句与返回结果语句的模式有很大不同。解析意味着您只需验证查询的语法。但是,要返回生成的模式涉及解析,那么您如何尝试此操作。
DataTable table = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select * from yourtable", "your connection string");
sda.FillSchema(table, SchemaType.Source);