This documentation is archived and is not being maintained.

Creating Connections to Oracle Databases

When connecting to an Oracle database use the Microsoft OLE DB Provider for Oracle.

Note   To enable drag and drop support from Server Explorer for Oracle databases, the data connection must use the Microsoft OLE DB Provider for Oracle.
Security Note   Storing connection-string details (such as the server name, user name, and password) can have implications for the security of your application. Using Windows Integrated Security is a more secure way to control access to a database. For more information, see Database Security.

There are two ways to connect to an Oracle database:

  • Visually, with design-time tools.
  • Programmatically.

Connecting to an Oracle Database in Server Explorer

To create a connection in Server Explorer

  1. In Server Explorer, right-click Data Connections and click Add Connection.
  2. In the Data Link Properties dialog box, click the Provider tab.
  3. Click the Microsoft OLE DB Provider for Oracle, then click Next.
  4. Type the server where the database you want to access is located.
  5. Type the password to use for authentication when you log on to the data source.
  6. Click OK.

Connecting to an Oracle Database from within your application

Creating connections to data sources from within your application can be accomplished visually using Visual Studio .NET designers, or programmatically by creating data connection objects in code.

Connecting to an Oracle Database Visually

Visual Studio supports creating data objects by dragging items from Server Explorer or the Data tab of the Toolbox onto your form or designer.

To create a connection from Server Explorer

  1. Create a Data Connection in Server Explorer to your Oracle database. For more information, see the above section titled "Connecting to an Oracle database in Server Explorer."
  2. Drag the connection onto your form or designer.
  3. An OracleConnection object appears in the component tray.
  4. Advance to the section "Next Steps" later in this topic.

To create a connection from the Data tab of the Toolbox

  1. Drag an OracleConnection onto your form or designer.

    An unconfigured OracleConnection object appears in the component tray.

  2. In the Properties window, click the ConnectionString property.
  3. Select an existing connection in the drop-down list, or click New Connection to open the Data Link Properties Dialog Box and configure a new connection.
  4. Advance to the section "Next Steps" later in this topic.

Connecting to an Oracle Database Programmatically

You can create an OracleConnection object directly in code.

To programmatically create a connection between your application and an Oracle database

  • The following code creates an OracleConnection object and sets the OracleConnection.ConnectionString property.
    ' Visual Basic
    Public Sub ConnectToOracle()
        Dim conn As New OracleClient.OracleConnection()
        ' TODO: Modify the connection string and include any
        ' additional required properties for your database.
        conn.ConnectionString = & _ 
        "Data Source=<oracle data source name>;Integrated Security=yes"
        Try
            conn.Open()
            ' Insert code to process data.
        Catch ex As Exception
            MessageBox.Show("Failed to connect to data source")
        Finally
            conn.Close()
        End Try
    End Sub
    
    // C#
    public void ConnectToOracle()
    {
        OracleClient.OracleConnection conn =
            new OracleClient.OracleConnection ();
        // TODO: Modify the connection string and include any
        // additional required properties for your database.
        conn.ConnectionString = "Data Source=" +
            "<oracle data source name>;Integrated Security=yes";
        try
        {
            conn.Open();
            // Insert code to process data.
        }
            catch (Exception ex)
        {
            MessageBox.Show("Failed to connect to data source");
        }
        finally
        {
            conn.Close();
        }
    }
    

Next Steps

Now that you have established a connection between your application and an Oracle database, you can create objects to fetch and update data. The following table provides links to some relevant topics.

ToSee
Fetch read-only dataRetrieving Data Using the DataReader
Fill a dataset with dataADO.NET Datasets
Execute SQL statements directlyPerforming Database Operations Directly

See Also

Connecting to Data Sources with ADO.NET | Adding New Data Connections in Server Explorer | Databases in Server Explorer | Oracle Databases | Visual Database Tools | Data Link Properties Dialog Box

Show: