Help with executing access query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Job Lot

    Help with executing access query

    I have written 6 Queries in an Access DB, which are executed in a For
    Each…Next loop to populate DataSet object. A new DataTable object is created
    in DataSet for each query, as follows

    Private Sub GetRawLiRatios( )
    Dim cnn As OleDbConnection
    Dim cmd As New OleDbCommand
    Dim dad As OleDbDataAdapte r
    Dim dstRawFigures As New DataSet

    Dim arrRatios As String() = [Enum].GetNames(GetTy pe(LiRatios))
    Dim obj As New DataStructureCo nverter

    Try
    '! Initialize OleDbConnection object.
    cnn = CreateConnectio n()

    '! Set connection property for OleDbCommand object.
    cmd.Connection = cnn

    '! Add parameters to the paramter collection.
    cmd.Parameters. Add("IndustryNa me", cboIndustry.Tex t)
    cmd.Parameters. Add("Year", nudYear.Value)

    '! Set the command type property of the command object.
    cmd.CommandType = CommandType.Sto redProcedure

    For Each ratio As String In arrRatios
    '! Set the command text property of the command object
    '! to the name of the query.
    cmd.CommandText = ratio

    '! Initialize data adapter object with command object.
    dad = New OleDbDataAdapte r(cmd)

    '! Fill the dataset.
    dad.Fill(dstRaw Figures, ratio)

    Next ratio

    Catch ioExcep As IO.IOException
    MessageBox.Show (ioExcep.Messag e, Application.Pro ductName,
    MessageBoxButto ns.OK, MessageBoxIcon. Error)

    Catch ex As Exception
    Call HandleException (Me.Name, ex, "GetRawLiRatios ")
    End Try

    End Sub

    Where arrRatios is an Enumeration which contains name of the queries in
    access db. If any of the queries get deleted from the access db, For
    Each...Next loop is exited and exception is thrown.

    Now I want my For Each...Next loop to continue processing for reaming
    queries in the db, rather than throwing an exception. How can I check whether
    queries exists in the db before using Fill method?

    Thanks

  • Paul Clement

    #2
    Re: Help with executing access query

    On Mon, 10 Jan 2005 23:11:01 -0800, "Job Lot" <JobLot@discuss ions.microsoft. com> wrote:

    ¤ I have written 6 Queries in an Access DB, which are executed in a For
    ¤ Each…Next loop to populate DataSet object. A new DataTable object is created
    ¤ in DataSet for each query, as follows
    ¤
    ¤ Private Sub GetRawLiRatios( )
    ¤ Dim cnn As OleDbConnection
    ¤ Dim cmd As New OleDbCommand
    ¤ Dim dad As OleDbDataAdapte r
    ¤ Dim dstRawFigures As New DataSet
    ¤
    ¤ Dim arrRatios As String() = [Enum].GetNames(GetTy pe(LiRatios))
    ¤ Dim obj As New DataStructureCo nverter
    ¤
    ¤ Try
    ¤ '! Initialize OleDbConnection object.
    ¤ cnn = CreateConnectio n()
    ¤
    ¤ '! Set connection property for OleDbCommand object.
    ¤ cmd.Connection = cnn
    ¤
    ¤ '! Add parameters to the paramter collection.
    ¤ cmd.Parameters. Add("IndustryNa me", cboIndustry.Tex t)
    ¤ cmd.Parameters. Add("Year", nudYear.Value)
    ¤
    ¤ '! Set the command type property of the command object.
    ¤ cmd.CommandType = CommandType.Sto redProcedure
    ¤
    ¤ For Each ratio As String In arrRatios
    ¤ '! Set the command text property of the command object
    ¤ '! to the name of the query.
    ¤ cmd.CommandText = ratio
    ¤
    ¤ '! Initialize data adapter object with command object.
    ¤ dad = New OleDbDataAdapte r(cmd)
    ¤
    ¤ '! Fill the dataset.
    ¤ dad.Fill(dstRaw Figures, ratio)
    ¤
    ¤ Next ratio
    ¤
    ¤ Catch ioExcep As IO.IOException
    ¤ MessageBox.Show (ioExcep.Messag e, Application.Pro ductName,
    ¤ MessageBoxButto ns.OK, MessageBoxIcon. Error)
    ¤
    ¤ Catch ex As Exception
    ¤ Call HandleException (Me.Name, ex, "GetRawLiRatios ")
    ¤ End Try
    ¤
    ¤ End Sub
    ¤
    ¤ Where arrRatios is an Enumeration which contains name of the queries in
    ¤ access db. If any of the queries get deleted from the access db, For
    ¤ Each...Next loop is exited and exception is thrown.
    ¤
    ¤ Now I want my For Each...Next loop to continue processing for reaming
    ¤ queries in the db, rather than throwing an exception. How can I check whether
    ¤ queries exists in the db before using Fill method?
    ¤
    ¤ Thanks

    You should be able to use the GetOleDbSchemaT able method of the OleDbConnection object. Can't recall
    exactly what the difference is but check for Procedures and Views:

    Dim DatabaseConnect ion As New System.Data.Ole Db.OleDbConnect ion

    Dim SchemaTable As DataTable

    DatabaseConnect ion.ConnectionS tring = "Provider=Micro soft.Jet.OLEDB. 4.0;" & _
    "Data Source=E:\My Documents\db1.m db"

    DatabaseConnect ion.Open()

    'Retrieve schema information about Catalog.

    SchemaTable =
    DatabaseConnect ion.GetOleDbSch emaTable(System .Data.OleDb.Ole DbSchemaGuid.Pr ocedures, _
    New Object() {Nothing, Nothing, "ValidateUser"} )

    'SchemaTable =
    DatabaseConnect ion.GetOleDbSch emaTable(System .Data.OleDb.Ole DbSchemaGuid.Vi ews, _
    ' New Object() {Nothing, Nothing, "Query2"})

    If SchemaTable.Row s.Count <> 0 Then
    Console.WriteLi ne("QueryDef " & SchemaTable.Row s(0)!PROCEDURE_ NAME.ToString & " does not
    exist")
    Else
    Console.WriteLi ne("QueryDef does not exist")
    End If

    'displays the properties
    DataGrid1.DataS ource = SchemaTable

    DatabaseConnect ion.Close()


    Paul ~~~ pclement@amerit ech.net
    Microsoft MVP (Visual Basic)

    Comment

    Working...