Results 1 to 4 of 4

Thread: Will the data in DataTable be updated according to continuous query?

  1. #1
    candice is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jan 2007
    Posts
    30
    Reputation
    79

    Lightbulb Will the data in DataTable be updated according to continuous query?

    Hello guys,
    I'm now wirting a program that is expected to excute a conditional SQL query.
    following is part of my code:
    Code:
    
    Sub method()
    Dim dt As DataTable = GetData("
    select lotid from actl where eqpid='S103SIG' and lotid not in (select lotid from rpt_del_lot)
    ")
    IfNotdt.Rows(r).Item("lotid")IsNothingThen
    GetData("SQL goes on here")
    else 
    GetData("SQL goes on here")
    EndIf
    
    Code:
    
    PublicFunction GetData(ByVal selectCommand AsString) As DataTable
    Try
    
    Dim connectionString AsString = _
    " Data Source=singdb;User ID=mfg_rpt;Password=sentosa;Unicode=True;"
    ' Create a new data adapter based on the specified query. 
    Me.dataAdapter = New OracleDataAdapter(selectCommand, connectionString)
    
    Dim commandBuilder AsNew OracleCommandBuilder(Me.dataAdapter)
    ' Populate a new data table and bind it to the BindingSource. 
    Dim dt AsNew DataTable()
    dt.Locale = System.Globalization.CultureInfo.InvariantCulture
    Me.dataAdapter.Fill(dt)
    Return dt
    Catch ex As OracleException
    MessageBox.Show("To run this example, replace the value of the " + _
    "connectionString variable with a connection string that is " + _
    "valid for your system.")
    EndTry
    EndFunction
    
    I'm wondering if this code can run as expected. In other words, whether the previous data in datatable can be updated by the data got from the next "GetData" statement. And whether this condition goes the same in datagridview.
    Thank you very much!!

  2. #2
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,027
    Reputation
    1705
    This post doesnt make much sense to me, but i offer the following tips:

    *TableAdapters ususally clear a datatable before they fill it

    *If you write a code that fills a datatable, then skips over the datatable, filling more data, it may never finish

    *Dont write sqls like this:
    select lotid from actl where eqpid='S103SIG' and lotid not in (select lotid from rpt_del_lot)
    Write them like this instead:

    Code:
    select 
     a.lotid 
    from 
     actl a
     left outer join
     rpt_del_lot r
     on 
       r.lotid = a.lotid
     
    where 
     a.eqpid='S103SIG' and 
     r.lotid is null
    why? you want all lotids where eqpid is something, and those lots dont appear in rpt_del_lot. so, you should join the two tables together with a left join, where rpt_del_lot will be blank if it doesnt exist, and then look for that as a condition

    The optimizer can work with queries like this much more easily than a nOT IN.
    IN can be very inefficient. Dont use it unless it is for a list so small that you would be prepared to type it by hand.

  3. #3
    candice is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jan 2007
    Posts
    30
    Reputation
    79
    Thanks cjard,
    Ya, I realize my post makes no much sense too...But now I encounter a problem.
    My logic is:1, query data using SQL1
    2, if is not no rows selected then query data using SQL2
    3, Else query data using SQL3
    4, end if
    And I modified my function like following, which will return nothing when the exception being caught.
    Code:
    Public Function GetData(ByVal selectCommand As String) As DataTable 
            Dim dt As New DataTable() 
            Try 
                Dim connectionString As String = _ 
               " Data Source=singdb;User ID=mfg_rpt;Password=sentosa;Unicode=True;" 
     
                Me.dataAdapter = New OracleDataAdapter(selectCommand, connectionString) 
     
                Dim commandBuilder As New OracleCommandBuilder(Me.dataAdapter) 
                dt.Locale = System.Globalization.CultureInfo.InvariantCulture 
                Me.dataAdapter.Fill(dt) 
     
           Catch ex As OracleException 
                dt = Nothing 
            End Try 
            Return dt 
        End Function
    The submethod is:
    Code:
      Dim dt As DataTable 
            dt = GetData("SQL1") 
            If Not dt Is Nothing Then 
                dt = GetData("SQL2 goes on here") 
            Else 
                dt = GetData("SQL3 goes on here") 
            End If
    The result is if the 1st SQL got something, the 2nd SQL will run. And it works well.
    But if the 1st SQL got nothing, the 3rd SQL never carry on, I finnally got nothing. (The 3rd SQL can get something, it runs well by itself.)
    Why does this happen? Doea that mean once the OracleException is caught, the following getdata can't carry on? Or is there sth. wrong with my logic?
    Thank you for your help!!

  4. #4
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,027
    Reputation
    1705
    Erm

    I'm not sure but I think youre assuming that no_rows_returned is the same as nothing?

    It is perfectly possible that a query will return no rows, so you will get a datatable object which is Something, but it will have 0 rows

    You might do this:
    Code:
            Dim dt As New DataTable() 
            Try 
                Dim connectionString As String = _ 
               " Data Source=singdb;User ID=mfg_rpt;Password=*****;Unicode=True;" 
     
                Me.dataAdapter = New OracleDataAdapter(selectCommand, connectionString) 
     
                Dim commandBuilder As New OracleCommandBuilder(Me.dataAdapter) 
                dt.Locale = System.Globalization.CultureInfo.InvariantCulture 
                Me.dataAdapter.Fill(dt) 
     
           Catch ex As OracleException 
                MessageBox.Show(ex.Message) 'never hide the errors! log them!
            End Try 
            Return dt 
        End Function
    and then:
    Code:
            dt = GetData("SQL1") 
            If dt.Rows.Count > 0 Then 
                dt = GetData("SQL2 goes on here") 
            Else 
                dt = GetData("SQL3 goes on here") 
            End If
    But remember that whatever data is gotten by SQL1 is lost!


    Question.. why are you doing your data access with untyped datasets and connection strings code in your button handlers? Oracle works with DataSet Designer!

Bookmarks

Posting Permissions

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