1

How can I parse a SQL statement (for SQL Server) to extract columns and parameters info (Name, DataType) without using ActiveQueryBuilder.

Regards

4

2 回答 2

2

You can try with TSql100Parser class

Link :  http://msdn.microsoft.com/fr-fr/library/microsoft.data.schema.scriptdom.sql.tsql100parser.aspx

Sample :

 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;
 }
于 2012-09-25T10:03:46.597 回答
1

好吧,解析语句与取回结果语句的模式有很大不同。解析意味着您只需验证查询的语法。但是,要取回结果模式涉及解析,那么您试试这个怎么样。

DataTable table = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select * from yourtable", "your connection string");
sda.FillSchema(table, SchemaType.Source);
于 2012-09-25T10:11:11.773 回答