Re: Connecting to Oracle using VB??
Date: Fri, 31 Mar 2000 06:32:19 GMT
Message-ID: <TDXE4.75$lY5.3487_at_news2.randori.com>
Its very easy using ADO. You can download it as part of the MDAC at http://www.microsoft.com/data/download.htm. Only download version 2.5 is you are using Windows 2000.
Once you have it down and installed, you'll need to add a reference to ADO in your VB project. Go to the Project/References menu option. Check off the Microsoft ActiveX Data Objects 2.1 Library. You should also include the ADOVBS.BAS file into your project.
Now that VB knows about it you need to code it. The first step is creating a connection to the database. You do this by:
'* This creates the connection object.
Set adoMyConnection = New adodb.Connection
'* Now connect to the database.
gConnect = "DSN<your dsn name>;UID=<your username>;PWD=<your password>"
With adoMyConnection
.ConnectionTimeout = 2 .Open gConnect
End With
Now you can get data by creating recordsets. You do this by:
'* Create the recordset object.
dim rstTemp as ADODB.RecordSet
'* Now create your SQL statement to retrieve the data.
sSQL = "SELECT * FROM Authors"
Set rstTemp = OpenRdSetView(sSQL:=sSQL, adoConnection:=adoMyConnection)
- Note I use a function OpenRdSetView that runs the queries for me. This way I don't need to specify all the ADO parameters each time. Public Function OpenRdSetView(sSQL As String, Optional adoConnection As adodb.Connection)
'* Setup default error handler.
On Error GoTo OpenRecordsetViewError
'* Create a recordset that allows update. Set OpenRdSetView = CreateObject("ADODB.Recordset") OpenRdSetView.Open sSQL, adoConnection, _
adOpenForwardOnly, adLockReadOnly, adCmdText
Exit Function
OpenRecordsetViewError:
Call HandleError(ErrSource:="OpenRecordsetViewError", Err:=Err)
End Function
You can update your data just by issuing your SQL statements (INSERT, UPDATE, DELETE) through the connection you created (adoMyConnection). You do this by:
ExecuteSQL sSQL:=sSQL, adoConnection:=adoMyConnection
Here's that function too.
Public Function ExecuteSQL(sSQL As String, Optional sSource As String, Optional adoConnection As adodb.Connection, Optional lSuppressErrorHandler As Boolean) As String
'* Execute a SQL statement like DELETE or UPDATE where no recordset is returned.
'* Setup error handler.
If Not lSuppressErrorHandler Then
'* Setup error handler. On Error GoTo ExeSQLError
End If
'* Run the SQL statement.
adoConnection.Execute sSQL, , adCmdText
Exit Function
ExeSQLError:
Call HandleError(ErrSource:=sSource, Err:=Err)
ExecuteSQL = "Error"
End Function
That's it in a nutshell. Good luck.
baby <babypaw_at_sympatico.ca> wrote in message
news:EzWE4.32710$1C2.984014_at_news20.bellglobal.com...
> Can some body send me sample code on how to connect to Oracle using VB
> and so that It can add delete and change some information from the
> database.
> Please I need to known how for my self thank you or if not point me to
> where I can get the information
>
> thanks
> > >Received on Fri Mar 31 2000 - 08:32:19 CEST