When i check the result set. record count it returns -1 and while checking the recordset.EOF it returns true, thus the result set does not contain any value.
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL
Private Sub cmd_login_Click()
Dim pass As String
con.ConnectionString = "Provider=msdaora;Data Source=localhost;User Id=ams;Password=krishnan;"
con.Open
strSQL = "Select passwrd from ams.login_details where username = 'Admin'"
rs.Open strSQL, con
If Not (rs.EOF) Then
If rs("passwrd") = txt_pass.Text Then
MsgBox rs("passwrd")
End If
End If
rs.Close
con.Close
End Sub
I forget to commit the statements in Oracle Sql Developer that's why not data was fetched from the database, When i executed the commit statement, it's working fine.
So, I've been asked to update an old Classic ASP website. It did not use parameterized queries and there was very little input validation. To simplify things I wrote a helper function that opens a connection to the database, sets up a command object with any parameters, and creates a disconnected recordset [I think!?! :)] Here's the code:
Function GetDiscRS(DatabaseName, SqlCommandText, ParameterArray)
'Declare our variables
Dim discConn
Dim discCmd
Dim discRs
'Build connection string
Dim dbConnStr : dbConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & rootDbPath & "\" & DatabaseName & ".mdb;" & _
"Persist Security Info=False"
'Open a connection
Set discConn = Server.CreateObject("ADODB.Connection")
discConn.Open(dbConnStr)
'Create a command
Set discCmd = Server.CreateObject("ADODB.Command")
With discCmd
Set .ActiveConnection = discConn
.CommandType = adCmdText
.CommandText = SqlCommandText
'Attach parameters to the command
If IsArray(ParameterArray) Then
Dim cnt : cnt = 0
For Each sqlParam in ParameterArray
discCmd.Parameters(cnt).Value = sqlParam
cnt = cnt + 1
Next
End If
End With
'Create the Recordset object
Set discRs = Server.CreateObject("ADODB.Recordset")
With discRs
.CursorLocation = adUseClient ' Client cursor for disconnected set
.LockType = adLockBatchOptimistic
.CursorType = adOpenForwardOnly
.Open discCmd
Set .ActiveConnection = Nothing ' Disconnect!
End With
'Return the Recordset
Set GetDiscRS = discRS
'Cleanup
discConn.Close()
Set discConn = Nothing
discRS.Close() ' <=== Issue!!!
Set discRs = Nothing
Set discCmd = Nothing
End Function
My problem is that if I call discRS.Close() at the end of the function, then the recordset that is returned is not populated. This made me wonder if the recordset is indeed disconnected or not. If I comment that line out everything works properly. I also did some Response.Write() within the function using discRS values before and after setting ActiveConnection = Nothing and it properly returned the recordset values. So it seems to be isolated to discRS.Close().
I found an old article on 4guysfromrolla.com and it issues the recordset Close() in the function. I've seen the same thing on other sites. I'm not sure if that was a mistake, or if something has changed?
Note: I'm using IIS Express built into Visual Studio Express 2013
Disconnected recordset as far as I know refers to a recordset populated manually, not from database, e.g.used as multi dimensional array or kind of hash table.
So what you have is not a disconnected recordset since it's being populated from database, and by disposing its connection you just cause your code to not work properly.
Since you already have Set discConn = Nothing in the code you don't have to set it to nothing via the recordset or command objects, it's the same connection object.
To sum this all up, you should indeed get rid of tho following lines in your code:
Set .ActiveConnection = Nothing ' Disconnect!
discRS.Close() ' <=== Issue!!!
Set discRs = Nothing
Then to prevent memory leaks or database lock issues, you should close and dispose the recordset after actually using it in the code using the function e.g.
Dim oRS
Set oRS = GetDiscRS("mydatabase", "SELECT * FROM MyTable", Array())
Do Until oRS.EOF
'process current row...
oRS.MoveNext
Loop
oRS.Close ' <=== Close
Set oRS = Nothing ' <=== Dispose
To avoid all this hassle you can have the function return "real" disconnected recordset by copying all the data into newly created recordset. If relevant let me know and I'll come with some code.
In your function, you cannot close and clean up your recordset if you want it to be returned to the calling process.
You can clean up any connections and command objects, but in order for your recordset to be returned back populated, you simply do not close it or dispose of it.
Your code should end like this:
'Cleanup
discConn.Close()
Set discConn = Nothing
'discRS.Close()
'Set discRs = Nothing
'Set discCmd = Nothing
end function
In your code i can see:
Set .ActiveConnection = Nothing ' Disconnect!
So, this Recordset isn't already closed?
He is indeed using a disconnected recordset. I started using them in VB6. You set the connection = Nothing and you basically have a collection class with all the handy methods of a recordset (i.e. sort, find, filter, etc....). Plus, you only hold the connection for the time it takes to fetch the records, so back when Microsoft licensed their servers by the connection, this was a nice way to minimize how many userm were connected at any one time.
The recordset is completely functional, it's just not connected to the data source. You can reconnect it and then apply any changes that were made to it.
It was a long time ago, it seems that functionality has been removed.
You should use the CursorLocation = adUseClient. Then you can disconnect the recordset. I have created a function to add the parameters to command dictionary objects, and then return a disconnected recordset.
Function CmdToGetDisconnectedRS(strSQL, dictParamTypes, dictParamValues)
'Declare our variables
Dim objConn
Dim objRS
Dim Query, Command
Dim ParamTypesDictionary, ParamValuesDictionary
'Open a connection
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")
Set Command = Server.CreateObject("ADODB.Command")
Set ParamTypesDictionary = Server.CreateObject("Scripting.Dictionary")
Set ParamValuesDictionary = Server.CreateObject("Scripting.Dictionary")
Set ParamTypesDictionary = dictParamTypes
Set ParamValuesDictionary = dictParamValues
Query = strSQL
objConn.ConnectionString = strConn
objConn.Open
With Command
.CommandText = Query
.CommandType = adCmdText
.CommandTimeout = 15
Dim okey
For Each okey in ParamValuesDictionary.Keys
.Parameters.Append .CreateParameter(CStr(okey), ParamTypesDictionary.Item(okey) ,adParamInput,50,ParamValuesDictionary.Item(okey))
Next
.ActiveConnection = objConn
End With
objRS.CursorLocation = adUseClient
objRS.Open Command , ,adOpenStatic, adLockBatchOptimistic
'Disconnect the Recordset
Set objRS.ActiveConnection = Nothing
'Return the Recordset
Set CmdToGetDisconnectedRS = objRS
'Clean up...
objConn.Close
Set objConn = Nothing
Set objRS = Nothing
Set ParamTypesDictionary =Nothing
Set ParamValuesDictionary =Nothing
Set Command = Nothing
End Function
I need to use VBA to query a big data table (2.000.000 rows and 130 columns) from Oracle database and save it into text file.
The code I am using is the following
Dim DBConnection As ADODB.connection
Dim RecordSet As ADODB.RecordSet
'prepare string for connection
Dim strConnection As String
strConnection = "DRIVER=Oracle in OraClient11g_Home32;SERVER=" & database & " ;UID=" & username & ";PWD=" & password & ";DBQ=" & database & ";"
Set DBConnection = New ADODB.connection
'open connection
With DBConnection
.CommandTimeout = False
.ConnectionString = strConnection
.CursorLocation = adUseClient
.Open
End With
Set RecordSet = New ADODB.RecordSet
RecordSet.Open strSQLQuery, DBConnection, adOpenForwardOnly, adLockReadOnly
Do While RecordSet.EOF = False
str = ""
For x = 0 To RecordSet.Fields.Count - 1
If IsNumeric(RecordSet.Fields(x).Value) Then
dx = RecordSet.Fields(x).Value
str = str & Format(dx, "0.############") & delimiter
Else
str = str & RecordSet.Fields(x).Value & delimiter
End If
Next x
Print #FH, str
RecordSet.MoveNext
Loop
The problem is that probably ADO tries to store in the recordset all the queried data, which will be several GB of data, thus using too much RAM.
I need to find a way to limit the number of rows that are stored in RAM at one time, so that I can download and save as many row as I want without having any issue with the RAM.
I researched but I cannot find anything about this.
Thank you.
May be you should try setting CursorLocation to adUseServer...
This relates to my recent question: Force Oracle error on fetch
I am now able to reproduce a scenario where using ADO with the Oracle OLEDB Provider, I can force an error such as ORA-01722: invalid number to occur on calling Recordset.MoveNext However, this is not the error that is returned to the application. Instead, the application sees Provider error '80004005' Unspecified error. How can I get the application to see the real error from the database? This is with Oracle 10g (client and server), if it matters.
Sample code is roughly as follows:
Dim con As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
con.ConnectionString = "Provider=OraOLEDB.ORACLE;Data Source=xxx;User Id=yyy;Password=zzz"
con.CursorLocation = adUseServer
con.Open
Set cmd.ActiveConnection = con
cmd.CommandText = "select * from table(ret_err)"
cmd.Prepared = True
Set rs = cmd.Execute
While Not rs.EOF
rs.MoveNext
Wend
SQLCODE and SQLERRM should be returned with an SQL call.
It looks like ADO Error is the interface for accessing this.
I started a thread here, this is a continuation from that post: Calling a Module and a Class within a Form
I can't find the information I need to successfully query our database. What I need to do is to get a single value from a Pervasive database. I cannot find the list of ODBC commands to do this with.
Can someone please point me to some documentation that deals with these Pervasive ODBC commands? I'm using ADO ODBC for the connection.
EDIT:
I am also attempt to connect to a MySQL database as well and am hitting the same error. Here is a test Sub I created to call my MySQL function. The error is the same for MySQL as it is for Pervasive: "Object variable or With Block variable not set"
Public Sub testMe(id)
Dim MySqlConn As adodb.Connection 'Do I need this here or in the MySQL function?
Set MySqlConn = ConnectMySQL()
MySqlConn.Open "SELECT * FROM test", MySqlConn, adOpenDynamic, adLockOptimistic
End Function
First, you need to connect to the database. According to this website the connection string would be in this format:
Driver={Pervasive ODBC Client
Interface};ServerName=myServerAddress;dbq=#dbname;
Starting with code from your previous post, it could be extended like this:
Option Explicit
Public Function getEmployee() As String
Dim MyConnection As ADODB.Connection
Dim CM As ADODB.Command
Dim RS As ADODB.Recordset
Set MyConnection = ConnectSQL()
'one way using command objects
Set CM = New ADODB.Command
Set CM.ActiveConnection = MyConnection
CM.CommandType = adCmdText
CM.CommandText = "select * from <table>"
Set RS = New ADODB.Recordset
RS.Open CM, , adOpenStatic, adLockBatchOptimistic
'another way using just the connection
Set RS = MyConnection.Execute("select * from <table>")
'return the data
getEmployee = RS.Fields(0).Value
End Function
Public Function ConnectSQL() As ADODB.Connection
Set ConnectSQL = New ADODB.Connection
ConnectSQL.Open "Driver={MySQL ODBC Client Interface};ServerName=localhost;dbq=#testdb"
End Function