Results 1 to 13 of 13

Thread: Looping through Unknown Tables.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    21

    Looping through Unknown Tables.

    Good Evening everyone.

    I'm really not sure if this is a stupid question or not, but I have searched and searched, but cannot find any "Lamens" ways of accomplishing this task.

    I have been ordered by the powers to be, again, to give up my valuable private time, and again, learn how to and carry out the creation of a piece of software that will link to a database, list the tables in that particular database, and assign various information to various variables, to create degrees of confusion. *Whew*

    ***ken boss. Sorry, just had to get it off my chest.

    Anyway, now thats out of the way, this is what I'm trying ever so hard to acheive.

    I am using ActiveX Dataobjects 2.5

    I have a textbox, inputed with a location and database file name, creatind the connection string.

    I click a button, it connects to the desired database.
    Below this textbox, I would like to list all the TABLES within that database, creating the second step.

    I'm just not sure how to retrieve the "unknown" table identities.

    These will be required for the third step to create a recordset, for each of those tables, so the user can move and select data at his/her whim.

    Ignore the "StreamReader" Related stuff, and assume this is coded in VB6.
    VB Code:
    1. Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         Dim SR As StreamReader
    4.         SR = File.OpenText("C:\Documents and Settings\Owner\My Documents\Visual Studio Projects\DataImport\DataImport\ProfileList.txt")
    5.         Dim x As String
    6.         Dim y() As String
    7.         Dim i As Integer = 0
    8.  
    9.         While SR.Peek <> -1
    10.             x = SR.ReadLine()
    11.             y = Split(x, ",")
    12.  
    13.             ReDim Preserve DataProf(i)
    14.             DataProf(i).ID = y(0)
    15.             DataProf(i).CString = y(1)
    16.             DataProf(i).Table = y(2)
    17.             i = i + 1
    18.         End While
    19.         SR.Close()
    20.  
    21.         Dim CSt As String
    22.         For i = 0 To UBound(DataProf)
    23.             CSt = DataProf(i).CString
    24.             CS = New ADODB.Connection
    25.             CS.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source= " & CSt
    26.             CS.Open()
    27.  
    28. '---------Here is where the code will go to populate the TBL() array with the table ID's.
    29.             Dim TBL() As String
    30.  
    31.             RS = New ADODB.Recordset
    32.             'RS.Open("TBL() ARRAY GOES HERE!", CS, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockPessimistic, ADODB.CommandTypeEnum.adCmdTable)
    33.             If RS.RecordCount > 0 Then RS.MoveFirst()
    34.  
    35.  
    36.  
    37.             CS.Close()
    38.             CS = Nothing
    39.         Next
    40.  
    41.  
    42.  
    43.     End Sub


    Does anybody know a relatively simple way of retrieving the table names within the database???

    Thankyou so much in advance.

    -Honki.
    Last edited by honki; Jul 11th, 2006 at 10:36 AM.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Looping through Unknown Tables.

    Your "***ken boss" is right (sorry, no love this time). You're using .Net and still using legacy ADO. Using someof your own private time to learn .Net as a .Net programmer isn't asking much.

    Check the forums for ADO.Net. A few people have links in their signature with tutorials (Techgnome and jmchinnley come to mind).

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    21

    Re: Looping through Unknown Tables.

    I find using legacy ADO more robust and controllable. Generally bulletproof if all errors are handled correctly.

    I have been looking into ADO.net, but the boss has asked for the code to be "oldschool" so we could possibly utilise it later in a VBA environment.

    Just assume the DB lines of code, are coded in VB6.
    Last edited by honki; Jul 11th, 2006 at 10:34 AM.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Looping through Unknown Tables.

    All errors are handled only as long as the programmer handles them. .NET has a much better exception management system which allows the application to work gracefully. It even has transaction objects. I fell in love the first time I saw that.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    21

    Re: Looping through Unknown Tables.

    Sure, I can appreciate that, I have played with ADO.net....
    But I still am required (to avoid double handling) code this in a compatible language to use it in other and much older applications.

    Does anybody actually know how to solve my problem?

  6. #6
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Looping through Unknown Tables.

    Code:
    SELECT sysobjects.name [Table Name]
    FROM sysobjects
    WHERE sysobjects.name LIKE '%ias_%'
    Code:
    SELECT syscolumns.name [Field Name], sysobjects.name [Table Name]
    FROM syscolumns, sysobjects
    WHERE syscolumns.id = sysobjects.id
    AND syscolumns.name LIKE '%operationid%'
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    21

    Re: Looping through Unknown Tables.

    Thanks Dave.
    I really don't understand the code. I'm not so familiar with "SELECT" statements.
    Could you explain it?

  8. #8
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Looping through Unknown Tables.

    sysobjects is a table in each database that contains all the table names.

    syscolumns is a table in each database that contains all the field names.

    Using these 2 tables, as in my sample can reconstruct all the tables in the database.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    21

    Re: Looping through Unknown Tables.

    Thanks.

    These databases have no such tables and I didn't write these databases.
    They are databases provided from other applications, and the software I am writing is to be able to access information directly from them (I have permission BTW).

    For example, you have sent me a database you created, with tables you created.
    I can run my program, and I can list all the Table names within your database.

    Is this possible?

    Just like in Excel where you can import external data.
    You first select the Database,
    Then select the Table,
    Then select the fields you wish to import.

  10. #10
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Looping through Unknown Tables.

    It's possible to list the tables as I have shown, along with their fields. Importing data ad-hoc will not be so straightforward. It might be possible for some applications, but not possible for others, depending if their tables have foreign-key constraints, for example.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Looping through Unknown Tables.

    You are comfortable with LEGACY ADO but do not know SELECT statements?

    That seems like a contradiction...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Looping through Unknown Tables.

    What DBMS are you connecting to? The sysobjects and syscolumns tables Dave mentioned are from SQLServer and WILL be there if that's what you're using. If you're using a different DBMS then Dave's suggestion probably won't work but something similar will - you just need to find out whether there's a system table that contains the table names.

    You'll be able to get the column names by issuing a blank select (ie with a WHERE 1 = 0 clause) and iterating the fields collection.


    And as has been mentioned by others, if you want to do this you're REALLY going to have to learn how to do a SQL select. I personally find W3Schools provide the best tutorials on this kind of thing but you could also check the tutorials section of the forum, there's bound to be some good articles in there.

  13. #13
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Looping through Unknown Tables.

    MsAccess has similar tables, but you might have to alter the IDE to specifically view system tables. By default Access has them invisible I think.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width