DGML (directed graph markup language) is a graphing XML language supported by the new modeling tools in Visual Studio 2010.
You can create various code analysis graphs using Visual Studio 2010 Ultimate, and Visual Studio 2010 Premium includes a DGML viewer for displaying DGML documents, but does not include tools for creating them (other than the XML editor!). Unfortunately, for this release of Visual Studio, the Express versions and Professional does not include a DGML viewer. You can, however, share graphs by saving them as XPS documents directly from the DGML viewer. You can read more about the DGML format here.
The sample command line utility that comes with this blog demonstrates how you can very easily create a nice graph of the tables and table relationships in your database using DGML and SMO.
SQL Server Management Objects (SMO) are objects designed for programmatic management of Microsoft SQL Server. It allows you to inspect all objects that relate to SQL Server and manipulate these. For this sample, we only need access to the tables and foreign keys of the tables of a specific database.
var connectionString = @"Data Source=(local);Initial Catalog=AdventureWorksLT;Integrated Security=SSPI;";
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
var server = new Server(new ServerConnection(conn));
var database = server.Databases[conn.Database];
foreach (Table table in database.Tables)
{
System.Diagnostics.Debug.WriteLine(table.Schema + "." + table.Name;
foreach (ForeignKey key in table.ForeignKeys)
{
The code above demonstrates how to get a collection of Table objects for a database using SMO. In addition, each table object has a collection of ForeignKeys, that allow you to get information about each foreign key for the table.
A simple DGML document looks like this:
<DirectedGraph xmlns="http://schemas.microsoft.com/vs/2009/dgml">
<Nodes>
<Node Id="0" Label="tblDoctor" />
<Node Id="1" Label="tblDoctorLocation" />
<Node Id="2" Label="tblFavoriteDoctor" />
</Nodes>
<Links>
<Link Source="0" Target="1" Label="FK_tblDoctorLocation_tblDoctor" />
<Link Source="1" Target="2" Label="FK_tblFavoriteDoctor_tblDoctorLocation" />
</Links>
</DirectedGraph>
And looks like this in the DGML viewer:
This is graph for the AdventureWorksLT database:
The complete code for this sample is available for download here.
Hope you find it useful!