Wednesday, March 4, 2009

How to make Connection to Oracle from VB.NET

How to connect to Oracle Database using VB.NET

1. You can make a variable into your My Project > Settings
call this variable : sConnectionString
and in value connection string as shown below.

Source=(DESCRIPTION=(ADDRESS_
LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=servername.mycompany.com)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=dev.mycompany.com)));

Note: please make sure you put your servers information in : Host and Service_Name

Now go to the windows form or asp.net page:

Imports this into your page:

Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types

Note: if you can't find this in you project then you download this from oracel website :
Oracle Dll: Oracle.DataAccess.dll
version: 2.102.2.20

Declare Private Variable in your Form1.VB Page:

Private Shared sConnectionString As String = My.Settings.OracleConnectionString

Here is the function:
Public Function ProcessErrorFixDataCheck(ByVal itemNum As String) As Boolean
Dim sqlStr As String
Dim cmd As New Oracle.DataAccess.Client.
OracleCommand
Dim oraDa As OracleDataAdapter
Dim conn As OracleConnection = Nothing
Dim ds As Data.DataSet
Try

sqlStr = "select item_Num from itemTable where item_Num = '" & itemNum & "'"

' Initialize connection
oraDa = New OracleDataAdapter(sqlStr, sConnectionString)
conn = oraDa.SelectCommand.Connection
ds = New Data.DataSet()
oraDa.Fill(ds, "ItemCount")


If ds.Tables(0).Rows(0)(0) IsNot Nothing Then
Return True
Else
Return False
End If

Catch ex As Exception
Throw ex
Finally
cmd.Dispose()
conn.Close()
conn.Dispose()
GC.Collect()
End Try
End Function

No comments:

Post a Comment