SQLDMO

  • I am creating a database documentor. I am getting an error message (below) on the code below and need some help. I am using C#.Net to create the program. Any feedback would be great.

    Error message:

    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in DBDocumentor.exe

    Additional information: [SQL-DMO]The ID '5' was not found in the Tables collection.

    Code:

    for (int i = 1; i <= this.poSqlDMO.Databases.ItemByID(DatabaseNumber).Tables.Count-1; i++)

    {

    this.lstTables.Items.Add (this.poSqlDMO.Databases.ItemByID(DatabaseNumber).Tables.ItemByID(i).Name);

    }

    Thanks

    Jim


    Thanks

    Jim

  • Think your counter must be off. Tried using for/each?

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

  • Thanks for the feedback. I am new to the whole for each loop thing. would I use the code below?

    foreach (this.poSqlDMO.Databases.ItemByID(DatabaseNumber).Tables in this.poSqlDMO.Databases)

    {

    this.lstTables.Items.Add (this.poSqlDMO.Databases.ItemByID(DatabaseNumber).Tables.ItemByID(i).Name);

    }

    Thanks

    Jim


    Thanks

    Jim

  • Close, but I think you are doing more work than you need. Then again, I read C# about as well as I read Klingon. Here is similar code for VB6.

    dim db as database

    dim tbl as table

    for each db in server.databases

    print db.name

    for each tbl in db.tables

    print tbl.name

    next

    next

    No reason to use the ItemByID method unless you're looking for one specific db.

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

  • Andy,

    The program retrieves the databases and lists them in a list box. When the user clicks on a database the tables for that database are retrieved. Would the code be the following?

    dim db as database

    dim tbl as table

    for each db in server.databases(x)

    print db.name

    for each tbl in db.tables

    print tbl.name

    next

    next

    Thanks

    Jim


    Thanks

    Jim

  • Typically I'd do this:

    set odb=oserver.databases("Northwind")

    for each otbl in odb.tables

    etc, etc

    next

    set odb=nothing

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

  • Andy,

    Would the code below work in VB.Net?

    set odb=oserver.databases("Northwind")

    Thanks

    Jim


    Thanks

    Jim

  • Nope. Have to do it a bit differently:

    odb = oserver.Databases.Item("Pubs")

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply