Asp.net, vb.net, C#.net Resources

Welcome to Asp.net, vb.net, C#.net Resources Sign in | Join | Help
in Search

Need to retrieve the SQL string of an existing query...

Last post 07-24-2008, 4:05 PM by DuelMonster. 1 replies.
Sort Posts: Previous Next
  •  07-23-2008, 8:32 PM 2343

    Need to retrieve the SQL string of an existing query...

    Hi all,

    I have an access database that was created using a Database Generator app written in VB6.  This app creates the database based on a dictionary text file.

    I'm looking to upgrade this app to VB.net as I just can't stand VB6 anymore.  I'm also trying to add a function that will recreate the dictionary file in case the original get's lost (which is my main reason for doing this, as I have a Database without a dictionary and don't want to recreate this by hand).

    My problem is that I can't seem to find any info on how to retrieve the SQL string from a query that already exists in the database.  I need this to retrieve any joins it may contain.

    The VB6 app is using DAO with code similar to this:

            Dim dbTemp As Database
            Set dbTemp = DBEngine.OpenDatabase("PATH")
            Dim qryTemp As QueryDef
            Set qryTemp = m_dbData.QueryDefs("QUERY")
            Dim sSQLString As String = qryTemp.SQL

    If anyone knows how to achieve this in VB.net using ADO please let me know.

    Many thanks
    Andy

  •  07-24-2008, 4:05 PM 2344 in reply to 2343

    Re: Need to retrieve the SQL string of an existing query...

    Nevermind, I worked out how to do this my self... :-)

    For those of you that might be looking for the same solution here is the code I used to achieve it.

            Dim dtTables As DataTable = Nothing
            Dim dbCnct As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()
            Dim iIndx As Integer

            With dbCnct
                    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\~Trash\Database.mdb"
                    .Open()
                    dtTables = .GetSchema("Views")
                    .Close()
            End With

            For iIndx = 0 To dtTables.Rows.Count - 1 Step iIndx + 1
                    Debug.Print("Query Name: " & dtTables.Rows(iIndx)(2).ToString())
                    Debug.Print("Query: " & dtTables.Rows(iIndx)(3).ToString())
            Next
    I had the suspicion that the SQL statement should be stored in the Tables Schema so I wrote a for loop that printed out all the DataTable Row Items and "hey presto", there it was :-D This isn't the easiest of things to find information on...
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems