Export (0) Print
Expand All

OracleDataAdapter Class

Represents a set of data commands and a connection to a database that are used to fill the DataSet and update the database. This class cannot be inherited.

Namespace:  System.Data.OracleClient
Assembly:  System.Data.OracleClient (in System.Data.OracleClient.dll)

public sealed class OracleDataAdapter : DbDataAdapter, 
	IDbDataAdapter, IDataAdapter, ICloneable

The OracleDataAdapter serves as a bridge between a DataSet and database for retrieving and saving data. The OracleDataAdapter provides this bridge by using Fill to load data from the database into the DataSet, and using Update to send changes made in the DataSet back to the data source.

When the OracleDataAdapter fills a DataSet, it creates the necessary tables and columns for the returned data if they do not already exist. However, primary key information is not included in the implicitly created schema unless the MissingSchemaAction property is set to AddWithKey. You may also have the OracleDataAdapter create the schema of the DataSet, including primary key information, before filling it with data using FillSchema. For more information, see Adding Existing Constraints to a DataSet (ADO.NET).

NoteNote:

By default, numeric fields imported to a DataTable with Fill are mapped to OracleNumber objects. It is possible to overflow the OracleNumber, and throw an Oracle exception, by importing a non-integral numeric value that is either too large or too high precision for the OracleNumber's precision limitations. Refer to the description of OracleNumber for more information.

The OracleDataAdapter also includes the SelectCommand, InsertCommand, DeleteCommand, UpdateCommand, and TableMappings properties to facilitate loading and updating of data.

The .NET Framework Data Provider for Oracle does not support batched SQL statements. However, it does allow you to use multiple REF CURSOR output parameters to fill a DataSet, each in its own DataTable. You must define the parameters, mark them as output parameters, and indicate that they are REF CURSOR data types. Note that you cannot use the Update method when the OracleDataAdapter is filled using REF CURSOR parameters returned by a stored procedure, because Oracle does not provide the information necessary to determine what the table name and column names are when the SQL statement is executed. The following C# example assumes that you have created this stored procedure.

Create the following Oracle package on the Oracle server.

CREATE OR REPLACE PACKAGE CURSPKG AS 
   TYPE T_CURSOR IS REF CURSOR; 
   PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER, 
      IO_CURSOR OUT T_CURSOR); 
   PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR, 
      DEPTCURSOR OUT T_CURSOR);
END CURSPKG;
/

Create the following Oracle package body on the Oracle server.

CREATE OR REPLACE PACKAGE BODY CURSPKG AS 
    PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER, 
                               IO_CURSOR OUT T_CURSOR) 
    IS 
        V_CURSOR T_CURSOR; 
    BEGIN 
        IF N_EMPNO <> 0 THEN 
             OPEN V_CURSOR FOR 
             SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME 
                  FROM EMP, DEPT 
                  WHERE EMP.DEPTNO = DEPT.DEPTNO 
                        AND EMP.EMPNO = N_EMPNO; 
        ELSE 
             OPEN V_CURSOR FOR 
             SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME 
                  FROM EMP, DEPT 
                  WHERE EMP.DEPTNO = DEPT.DEPTNO; 
        END IF; 
        IO_CURSOR := V_CURSOR; 
    END OPEN_ONE_CURSOR; 
    PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR, 
                                DEPTCURSOR OUT T_CURSOR) 
    IS 
        V_CURSOR1 T_CURSOR; 
        V_CURSOR2 T_CURSOR; 
    BEGIN 
        OPEN V_CURSOR1 FOR SELECT * FROM EMP; 
        OPEN V_CURSOR2 FOR SELECT * FROM DEPT; 
        EMPCURSOR  := V_CURSOR1; 
        DEPTCURSOR := V_CURSOR2; 
    END OPEN_TWO_CURSORS; 
END CURSPKG;
 /

The following C# example demonstrates how you might obtain table and column information using the stored procedure.

// GetConnectionString() returns a connection string for
// the data source.

string connString = GetConnectionString();
DataSet ds = new DataSet();
OracleConnection conn = new OracleConnection(connString);
OracleCommand cmd = conn.CreateCommand();

cmd.CommandText = "CURSPKG.OPEN_TWO_CURSORS";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("EMPCURSOR", OracleType.Cursor).Direction = 
       ParameterDirection.Output;
cmd.Parameters.Add("DEPTCURSOR", OracleType.Cursor).Direction = 
       ParameterDirection.Output;

OracleDataAdapter da = new OracleDataAdapter(cmd);
da.TableMappings.Add("Table", "Emp");
da.TableMappings.Add("Table1", "Dept");
da.Fill(ds);

ds.Relations.Add("EmpDept", ds.Tables["Dept"].Columns["Deptno"], 
      ds.Tables["Emp"].Columns["Deptno"], false);

The following Visual Basic example demonstrates how to use an OracleDataAdapter to fill a DataSet using an Oracle REF CURSOR. These examples use tables that are defined in the Oracle Scott/Tiger schema, and require the following PL/SQL package and package body. You must create these on your server to use the examples.

Create the following Oracle package on the Oracle server.

CREATE OR REPLACE PACKAGE CURSPKG AS 
   TYPE T_CURSOR IS REF CURSOR; 
   PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER, 
      IO_CURSOR OUT T_CURSOR); 
   PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR, 
      DEPTCURSOR OUT T_CURSOR);
END CURSPKG;
/

Create the following Oracle package body on the Oracle server.

CREATE OR REPLACE PACKAGE BODY CURSPKG AS 
    PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER, 
                               IO_CURSOR OUT T_CURSOR) 
    IS 
        V_CURSOR T_CURSOR; 
    BEGIN 
        IF N_EMPNO <> 0 THEN 
             OPEN V_CURSOR FOR 
             SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME 
                  FROM EMP, DEPT 
                  WHERE EMP.DEPTNO = DEPT.DEPTNO 
                        AND EMP.EMPNO = N_EMPNO; 
        ELSE 
             OPEN V_CURSOR FOR 
             SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME 
                  FROM EMP, DEPT 
                  WHERE EMP.DEPTNO = DEPT.DEPTNO; 
        END IF; 
        IO_CURSOR := V_CURSOR; 
    END OPEN_ONE_CURSOR; 
    PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR, 
                                DEPTCURSOR OUT T_CURSOR) 
    IS 
        V_CURSOR1 T_CURSOR; 
        V_CURSOR2 T_CURSOR; 
    BEGIN 
        OPEN V_CURSOR1 FOR SELECT * FROM EMP; 
        OPEN V_CURSOR2 FOR SELECT * FROM DEPT; 
        EMPCURSOR  := V_CURSOR1; 
        DEPTCURSOR := V_CURSOR2; 
    END OPEN_TWO_CURSORS; 
END CURSPKG;
 /

This Visual Basic example executes a PL/SQL stored procedure that returns two REF CURSOR parameters, and fills a DataSet with the rows that are returned.

' GetConnectionString() returns a connection string for
' the data source.

    Dim connString As New String(GetConnectionString())
    Dim ds As New DataSet()
    Dim conn As New OracleConnection(connString)
    Dim cmd As OracleCommand = conn.CreateCommand()

    cmd.CommandText = "CURSPKG.OPEN_TWO_CURSORS"
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.Add("EMPCURSOR", OracleType.Cursor).Direction = _
       ParameterDirection.Output
    cmd.Parameters.Add("DEPTCURSOR", OracleType.Cursor).Direction = _
       ParameterDirection.Output

    Dim da As New OracleDataAdapter(cmd)
    da.TableMappings.Add("Table", "Emp")
    da.TableMappings.Add("Table1", "Dept")
    da.Fill(ds)

    ds.Relations.Add("EmpDept", ds.Tables("Dept").Columns("Deptno"), _
      ds.Tables("Emp").Columns("Deptno"), False)

After using the OracleDataAdapter to perform a Fill or FillSchema operation, the DataColumn.ReadOnly property always returns false, regardless of whether a column can be updated or not, because the Oracle server does not return this information.

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Data.Common.DataAdapter
        System.Data.Common.DbDataAdapter
          System.Data.OracleClient.OracleDataAdapter

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft