Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LinqPad with SMO

I am trying to use the SMO for Sql Server 2008 R2 Standard, but I am running into an issue whenever I try to Dump an object.

Error

The relevant code:

void Main()
{
    var connectionString = @"Server=(local);Trusted_Connection=True;";
    Server server = new Server(new ServerConnection(new SqlConnection(connectionString)));
    server.ConnectionContext.Connect();     

    server.Dump(); //Error      

    Database database = new Database(server, "master");
    database.Refresh();

    database.Dump(); // Error

    IEnumerable<Table> tables = database.Tables.Cast<Table>();

    tables.Dump(); //Error
}

AssembliesIncludes

Edit:

A work around that I found is to use the Dump method with a fixed recursion depth e.g. Dump(1), but the exception is at a different level for each object.

like image 726
Dustin Kingen Avatar asked Mar 12 '13 13:03

Dustin Kingen


1 Answers

What's happening is that the act of calling GetEnumerator on one of the SMO object properties is throwing an exception and LINQPad responds by dumping only that exception, rather than the rest of the object graph.

I consider this a bug in LINQPad and so have fixed it for the next beta build.

However, even with this fixed, you'll find that calling Dump() on a SMO object will take forever because it has so many properties that take ages to enumerate. So you'll have to either limit the recursion depth, or call Dump(true) which is equivalent to clicking the 'Results to DataGrids' button. Dumping results to data grids avoids the problem because the grids render data lazily.

like image 58
Joe Albahari Avatar answered Oct 09 '22 19:10

Joe Albahari