Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Last active December 13, 2015 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yetanotherchris/4952768 to your computer and use it in GitHub Desktop.
Save yetanotherchris/4952768 to your computer and use it in GitHub Desktop.
Printing all tables in a database with Microsoft.SqlServer.Management.Smo
// From microsoft.sqlserver.smo.dll
using Microsoft.SqlServer.Management.Smo;
void PrintTables()
{
Server server = new Server("(local)");
server.ConnectionContext.ConnectAsUserName = "sa";
server.ConnectionContext.ConnectAsUserPassword = "Passw0rd";
Database db = server.Databases["mydatabase"];
foreach (Table table in db.Tables)
{
Console.WriteLine(string.Format("[{0}]", table.Name));
foreach (Column column in table.Columns)
{
Console.WriteLine(string.Format("\t{0} {1} {2} {3}",
column.Name,
column.DataType.Name,
column.Default,
column.DataType.MaximumLength));
}
Console.WriteLine("");
}
Console.Read();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment