This repository contains an example C# application using SQL Assessment API. The application uses popular SQL Management Objects (SMO) library. WHile it was not required to invoke assessment, SMO gives a convenient object-oriented API for managing SQL Server objects. See Service based SQL Assessment for an example of non-SMO application.
To use SQL Assessment API this sample project references Microsoft.SqlServer.SqlManagementObjects and Microsoft.SqlServer.Assessment Nuget packages.
A typical SQL Assessment workflow consists of three stages:
-
Establish a connection to a SQL Server and select target instance or database (Microsoft.Data.SqlClient, SQL Management Objects, SQL Tools Service, PowerShell).
-
(Optional) Create SQL Assessment check list. A default check list is used if this step was skipped.
-
Invoke assessment on the target object selected at step 1.
SQL Management Objects is a handy tool for accessing and managing SQL SErver objects. This sample application connects to a local SQL Server instance with a short statement:
var target = new Server();
To assess a database replace this line with the following snippet:
var target = new Server().Databases["MyDatabase"];
See SMO documentation for more details on connecting to a server or a database.
SQL Assessment asynchronous code returns a list of SQL Assessment results. Each result can be an assessment note, warning, or error. Every assessment result contains a message to the user with a recommendation. Each assessment note is associated to the corresponding check from the list.
List<IAssessmentResult> results = await target.GetAssessmentResultsList();
SQL Assessment API gives a collection of checks currently available for given target. Selected checks may be passed to GetAssessmentResultsList
.
IEnumerable<ICheck> availableChecks = target.GetAssessmentItems();
Select checks to be run.
var checklist = availableChecks.First(5);
var results = await target.GetAssessmentResultsList(checklist);
SQL Assessment check may be tagged with one or more category names. Checks from default check list always have "DefaultRuleset" tag and at least one area tag. "DefaultRuleset" denotes a check produced by default ruleset supplied with SQL Assessment API. Area tags may be "Performance", "Security", "Memory", "Deprecated", "Storage", etc.
var checklist = target.GetAssessmentItems("Performance", "Security");
var results = await target.GetAssessmentResultsList(checklist);
This is equivalent to the following code:
var results = await target.GetAssessmentResultsList("Performance", "Security");
Checks are generated by a SQL Assessment engine for every target. The engine uses rules from one or more rule sets. Rule sets are collected on a stack. A rule set may override rules from an underlying rule set. A ruleset may be constructed with a C# code, but we recommend using declarative JSON format. For more details on rule set files see SQL Assessment API GitHub page.
using (var reader = File.OpenText("my ruleset.json"))
{
SqlAssessmentExtensions.Engine.PushRuleFactoryJson(reader);
}
SQL Tools Service is a JSON-RPC service over stdio. SQL assessment takes the same three steps: connect, select checks, run.
In the following examples adjust Content-Length
value according to actual JSON length including CR or CRLF line endings.
Content-Length:267
{
"jsonrpc": "2.0",
"id": "12",
"method": "connection/connect",
"params": {
"ownerUri": "my connection",
"connection": {
"serverName": "(local)",
"authenticationType": "Integrated"
}
}
}
See SQL Tools Service documentation for more details on connecting to a server or a database.
Use targetType
to select target type:
-
SQL Server instance.
-
SQL Server database. In this case provide database name while connecting to the target.
Content-Length:181
{
"jsonrpc": "2.0",
"id": "12",
"method": "assessment/getAssessmentItems",
"params": {
"targetType": 1,
"ownerUri": "my connection"
}
}
Sample output:
{
"jsonrpc": "2.0",
"id": "12",
"result": {
"success": true,
"errorMessage": null,
"items": [
{
"rulesetVersion": "1.0.280",
"rulesetName": "Microsoft ruleset",
"targetType": 1,
"targetName": "MYSERVER",
"checkId": "TF174",
"tags": [
"DefaultRuleset",
"TraceFlag",
"Memory",
"Performance"
],
"displayName": "TF 174 increases the plan cache bucket count",
"description": "Trace Flag 174 increases the SQL Server ...",
"helpLink": "https://docs.microsoft.com/sql/t-sql/ ...",
"level": "Information"
},
...
]
}
}
Use the same targetType
.
Content-Length:169
{
"jsonrpc": "2.0",
"id": "12",
"method": "assessment/invoke",
"params": {
"targetType": 1,
"ownerUri": "my connection"
}
}
Sample output:
{
"jsonrpc": "2.0",
"id": "12",
"result": {
"success": true,
"errorMessage": null,
"items": [
{
"message": "Enable trace flag 834 to use large-page allocations to improve analytical and data warehousing workloads",
"kind": 0,
"timestamp": "2020-11-09T22:46:36.5529014+03:00",
"rulesetVersion": "1.0.280",
"rulesetName": "Microsoft ruleset",
"targetType": 1,
"targetName": "MYSERVER",
"checkId": "TF834",
"tags": [
"DefaultRuleset",
"TraceFlag",
"Performance",
"Memory",
"ColumnStore"
],
"displayName": "TF 834 enables large-page allocations",
"description": "Trace Flag 834 causes the server ...",
"helpLink": "https://support.microsoft.com/kb/3210239",
"level": "Information"
},
...
]
}
}