Connection to Oracle DB and Query Execution

 

 

Most application need, to store they data, a DataBase. Here it is the code to connect to an Oracle DB, execute a query and read the result.

I use the ADODB technology to create Connection and RecordSet objects.

 

 

'***********************************************

'**********     MAIN PROGRAM   *********************

'***********************************************

Const query = "Select * from MYTABLE"

'RECORDSET Constants
Const adOpenStatic     = 3
Const adLockOptimistic = 3
Const adUseClient      = 3

 

Dim istanza_db, usr, psw, objConnection, objRecordSet

 

istanza_db = inputbox("Insert the DB instance:", "DB Name","IST1")
if istanza_db = "" then
    wscript.quit
end if

usr = inputbox("Insert user:", "User", "user01")
if usr = "" then
    wscript.quit
end if

psw = inputbox("Insert Password:", "Password","")


'***********************************************************

'**********    COMMANDS FOR DB ORACLE  CONNECTION    *************
'***********************************************************

'ConnectionString for Db ORACLE
ConnString = "Provider=msdaora; Data Source=" & istanza_db & "; User Id=" & usr & "; Password=" & psw & ";"

'Creation of ADODB.Connection object
Set objConnection = CreateObject("ADODB.Connection")

    if err.number<>0 then
       msgbox "error during creation of ADODB.Connection"
       msgbox err.description
       wscript.quit
    end if

'Open the Connection
objConnection.Open ConnString

'************************************************

'***********************************************************

'** COMMANDS FOR THE QUERY EXECUTION THROUGH  RECORDSET  OBJECT **
'***********************************************************

Set objRecordset = CreateObject("ADODB.Recordset")

objRecordset.CursorLocation = adUseClient
objRecordset.CursorType     = adOpenStatic
objRecordset.LockType       = adLockOptimistic


'query execution
objRecordset.Open query, objConnection


'Check that the query returns something
if objRecordSet.RecordCount > 0 then

        'Move to the 1st record of the recordset
        objRecordSet.MoveFirst

 'do a cycle for all the records of the recordset
 do while Not(objRecordSet.EOF)
           'cycle for all columns (fields)
           for i = 0 to objRecordSet.Fields.Count - 1
                'Display value for each field of the RecordSet
  msgbox objRecordSet.Fields.Item(i)
           next
           'move to the next record
           objRecordSet.MoveNext
        loop

end if


'****************************************************

'*************************
'DESTRUCTION OF THE  OBJECTS
'*************************

set objRecordSet = Nothing
set objConnection = Nothing

'END OF THE PROGRAM

 

______________________________________________________________________

 

Pag: <<    <    >    >>