T-SQL Parser


// /r:"C:\Program Files\Microsoft Visual Studio 10.0\VSTSDB\Deploy\Microsoft.Data.Schema.ScriptDom.dll";"C:\Program Files\Microsoft Visual Studio 10.0\VSTSDB\Deploy\Microsoft.Data.Schema.ScriptDom.Sql.dll"
namespace Microshaoft
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using Microsoft.Data.Schema.ScriptDom;
    using Microsoft.Data.Schema.ScriptDom.Sql;
    public class Class1
    {
        static void Main(string[] args)
        {
            //SqlScriptGeneratorOptions options = new SqlScriptGeneratorOptions();
            //options.SqlVersion = SqlVersion.Sql100;
            //options.KeywordCasing = KeywordCasing.Uppercase;
            //Sql100ScriptGenerator scriptGenerator = new Sql100ScriptGenerator(options);
            SqlScriptGenerator scriptGenerator = new Sql100ScriptGenerator();
            TSqlParser parser = new TSql100Parser(false);
            IList<ParseError> errors;
            string sql =
@"
                    --SELECT * FROM [from] WHERE ID='1'
                    select a.id,MAX(a.name),a.Name
                    from sysobjects aa
                    left join syscomments b
                    on a.id = b.id
                    where a.xtype = 'p'
                    and b.[text] like '% exec%'
                    group by a.id
";
            using (StringReader reader = new StringReader(sql))
            {
                IScriptFragment fragment = parser.Parse(reader, out errors);
                if (errors != null && errors.Count > 0)
                {
                    foreach(ParseError pe in errors)
                    {
                        Console.WriteLine(pe.Message);
                    }
                }
                else
                {
                    string script;
                    scriptGenerator.GenerateScript(fragment, out script);
                    Console.WriteLine(script);
                }
            }
            Console.WriteLine(Environment.Version.ToString());
        }
    }
}

posted @ 2011-02-06 02:12  于斯人也  阅读(863)  评论(1编辑  收藏  举报