Click or drag to resize
How To: Extend Analysis Template with Visual Studio

This topic describes how to create a new SQL Enlight analysis template that extends the default template.

  1. Create a new Class Library C# project named MyCompany.SqlEnlight.Analysis.Rules.

    Note Note

    The project have to be created targeting .NET Framework 2.0.

  2. Change the Default namespace from MyCompany.SqlEnlight.Analysis.Rules to MyCompany.SqlEnlight.Analysis.

    This should be done to ensure correct resolution of the rule embedded resources names.


  3. Add the following assembly references:

    • Ubitsoft.SqlEnlight.Model
    • Ubitsoft.SqlEnlight.Analysis.Rules

    The assemblies are located in the SQL Enlight installation folder.

  4. Rename the Class1.cs to CustomAnalysisTemplate.cs.

  5. Modify the class and add replace the content of the CustomAnalysisTemplate.cs file with the following code:

     1using System;
     2using System.Collections.Generic;
     3using System.Text;
     4using Ubitsoft.SqlEnlight.Analysis;
     5using Ubitsoft.SqlEnlight.Model.Analysis;
     7namespace MyCompany.SqlEnlight.Analysis.Rules
     9    public class CustomAnalysisTemplate : BasicAnalysisTemplate
    10    {
    11        public CustomAnalysisTemplate()
    12            : base()
    13        {
    14        }
    16        /// <summary>
    17        /// The initialization is done in the base class constructor.
    18        /// </summary>
    19        protected override void InitializeTemplate()
    20        {
    21            base.InitializeTemplate();
    22        }
    23    }
  6. Add new folders to the project Rules and Rules\TestScripts.

  7. Add new Batch Analysis Rule to the Rules folder.


    Note Note

    Make sure to set the desired name of the rule at this step in order to avoid later difficulties renaming the rule files.

  8. Add new Text file to the Rules\TestScripts folder and rename it to Rule1.sql (the name must be same as the rule name) and change its Build Action to Embedded Resource.


The structure of the project after the modifications should look like this:


Modify the CustomAnalysisTemplate’s InitializeTemplate method and add initialization code for the new rule:

 1/// <summary>
 2/// The initialization is done in the base class constructor.
 3/// </summary>
 4protected override void InitializeTemplate()
 6    base.InitializeTemplate();
 8    Rule1 rule1 = new Rule1();
10    // Add the new rule to the collection of existing rules.
11    this.Rules.Add(rule1.Name, rule1);
13    // Create a new group and add it to the groups collection.
14    this.Groups.Add("CustomRules", new AnalysisGroup("CustomRules", "My Rules", "My of SQL analysis rules.", new string[] { rule1.Name }, true));

From here you can continue and implement the rule that we created for the template.

For how to do this, you can see in the next tutorial: How To: Create Analysis Rule with Visual Studio.

See Also