Top

Tags


Roadkill .NET Wiki

Google ads

Recommended reading


Search

Thursday
Feb182010

Printing all tables in a database with Management.Smo

The Microsoft.SqlServer.Management.Smo namespace and assembly is used by Management Studio for its interogation of the database. If you have Management Studio installed you can use this in your own applications, a simple example of which is shown below.

// 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();
}

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
« String.Equals(), == and String.Compare() | Main | .NET Formatting Strings Reference »