Domanda

I have problem to query database from reader's loop:

    Using cn As NpgsqlConnection = getConnection()
        Using t As NpgsqlTransaction = cn.BeginTransaction()
           Using cmd As New NpgsqlCommand("SELECT tab_id, i1, d1, rez1, b FROM " & myfirstTable & " WHERE bdoc='" & bdoc.ToString & "' ORDER BY b", cn)
                Using reader1 As NpgsqlDataReader = cmd.ExecuteReader()

    --------------> ''GOES HERE
    |               While (reader1.Read())
    |                   Dim sifra As Integer = CInt(reader1("sifra"))
    | 
    |                       Using icmd As New NpgsqlCommand("SELECT dtbl_id FROM " & mysecondTable & " WHERE dtbl_id>" & firstfree.ToString & " AND kni::int=0 ORDER BY dtbl_id LIMIT 1", cn)
    |               
    --------------------------- ''FROM HERE
                                firstfree = CInt(icmd.ExecuteScalar())
                            End Using ''cmd

                    End While
                End Using ''reader
            End Using ''cmd
            t.Commit()
        End Using
    End Using

While reader's.read loop I have to get integer firstfree from new query to database.
My command work's good by testing in pgAdmin.
But in showed program don't.
When firstfree = CInt(icmd.ExecuteScalar()) is going to be executed ''FROM HERE program jumps to new loop step ''GOES HERE

Is something obvious incorrect here and how to get that working?

Here is getConnection function.

Public Function getConnection() As NpgsqlConnection

    Dim chkCon As New NpgsqlConnection(String.Format( _
                      "Server={0};Port={1};User Id={2};Password={3};Database={4};Preload Reader = true;", _
                      dbServer, dbPort, dbUser, dbPass, mydatabase))
    Try
        chkCon.Open()
    Catch ex As Exception
        Return Nothing
    End Try

    Return chkCon
End Function
È stato utile?

Soluzione

I don't see what your language is, etc. but it does seem to me you have two obvious typos in your loops.

For example, should:

 End Using ''cmd

be

 End Using ''icmd

and should

 End Using ''reader

be

 End Using ''reader1

At any rate this looks like a loop syntax issue, so I would look at debugging as a .net issue rather than a PostgreSQL issue.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top