我从阅读器循环中查询数据库时遇到问题:
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
虽然 reader's.read 循环我必须先从新查询到数据库中获取整数。
通过在 pgAdmin 中进行测试,我的命令工作很好。
但在显示的程序中没有。
当 firstfree = CInt(icmd.ExecuteScalar()) 将被执行时 ''FROM HERE 程序跳转到新的循环步骤 ''GOES HERE
这里有什么明显不正确的地方吗?如何让它发挥作用?
这是getConnection函数。
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