Reputation: 492
How can I parse a SQL statement (for SQL Server) to extract columns and parameters info (Name, DataType) without using ActiveQueryBuilder.
Regards
Upvotes: 1
Views: 3163
Reputation: 67898
Well, parsing the statement is much different than getting back the schema of the resulting statement. Parsing would mean your just validating the syntax of the query. However, to get back the resulting schema involves parsing so how about you try this.
DataTable table = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select * from yourtable", "your connection string");
sda.FillSchema(table, SchemaType.Source);
Upvotes: 1
Reputation: 28970
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;
}
Upvotes: 2