Skip to content

Commit

Permalink
Added code example.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-dunwiddie authored Dec 14, 2019
1 parent 391a68c commit 57278c7
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,53 @@ Available on Nuget, [TSQL.Depends](https://www.nuget.org/packages/TSQL.Depends/)

Install-Package TSQL.Depends

## Example Dependency Parsing

```csharp
using System;
using System.Collections.Generic;
using System.Data.SqlClient;

using TSQL.Depends;

class Program
{
static void Main(string[] args)
{
string AdventureWorks = "Data Source=.;Initial Catalog=AdventureWorks2017;Integrated Security=True";

// get the definition of the stored proc dbo.uspGetBillOfMaterials from the AdventureWorks database
string procDefinition = null;

using (SqlConnection conn = new SqlConnection(AdventureWorks))
using (SqlCommand getProcDefinition = new SqlCommand(@"
SELECT
m.[definition]
FROM
sys.sql_modules m
WHERE
m.object_id = OBJECT_ID('dbo.uspGetBillOfMaterials');", conn))
{
conn.Open();

procDefinition = getProcDefinition.ExecuteScalar().ToString();
}

TSQLDependencyParser parser = new TSQLDependencyParser(AdventureWorks);

List<TSQLObjectDependency> dependencies = parser.GetDependencies(procDefinition);

// write out all the object references found within the script
foreach (var dependency in dependencies)
{
Console.WriteLine($"{dependency.Type} {dependency.DatabaseName}.{dependency.SchemaName}.{dependency.Name}");
}
}
}
```

```
StoredProcedure AdventureWorks2017.dbo.uspGetBillOfMaterials
Table AdventureWorks2017.Production.BillOfMaterials
Table AdventureWorks2017.Production.Product
```

0 comments on commit 57278c7

Please sign in to comment.