Connect to Oracle from .NET

We don't have a lot of experience connecting to Oracle. We've done it ... once now.  : )

But my policy is to blog all new code, so that if I need it again it'll be here. 

So... Connecting to Oracle from .NET:

Related Articles

... and you 'll find more on the NET Development Menu

Two similar pieces of code, the first one returns a data set, the second just sends an update. 

Public Shared Sub OracleConn(strServer As String, strDatabase As String)
 
    Dim oDT As DataTable
 
    ' Create and open the  connection in a using block. This
    ' ensures that all resources will be closed and disposed
    ' when the code exits.
    Using oConn As New OleDb.OleDbConnection("Provider=OraOLEDB.Oracle.1;Persist Security Info=False;Data Source=dba2;User Id=****; Password=**********;")
 
        Dim oCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand()
 
        oCmd.Connection = oConn
        oCmd.CommandText = "Select * from BGGS_NEW.ENV_GLBL_CUST WHERE CUST_ID='5503'"
        oCmd.CommandType = CommandType.Text
 
        ' Open the connection in a try/catch block.
        ' Create and execute the DataReader, writing the result
        ' set to the console window.
        Try
            oConn.Open()
            Dim oDA As New OleDb.OleDbDataAdapter()
            oDA.SelectCommand = oCmd
            Dim oDS As New DataSet
 
            oDA.Fill(oDS)
            oDT = oDS.Tables(0)
 
        Catch ex As OleDb.OleDbException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
 
    End Using
 
End Sub
 
Public Shared Sub OracleUpdate()
 
    ' Create and open the  connection in a using block. This
    ' ensures that all resources will be closed and disposed
    ' when the code exits.
    Using oConn As New OleDb.OleDbConnection("Provider=OraOLEDB.Oracle.1;Persist Security Info=False;Data Source=dba2;User Id=****; Password=**********;")
 
        Dim oCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand()
 
        oCmd.Connection = oConn
        oCmd.CommandText = "update BGGS_NEW.ENV_GLBL_CUST set status = 'A' WHERE CUST_ID='5503'"
        oCmd.CommandType = CommandType.Text
 
        ' Open the connection in a try/catch block.
        Try
            oConn.Open()
            oCmd.ExecuteNonQuery()
 
        Catch ex As OleDb.OleDbException
            Console.WriteLine(ex.Message)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
 
    End Using
 
End Sub

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences