Redigera

OdbcDataAdapter Class

Definition

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

public ref class OdbcDataAdapter sealed : System::Data::Common::DbDataAdapter, ICloneable
public ref class OdbcDataAdapter sealed : System::Data::Common::DbDataAdapter
public sealed class OdbcDataAdapter : System.Data.Common.DbDataAdapter, ICloneable
public sealed class OdbcDataAdapter : System.Data.Common.DbDataAdapter
type OdbcDataAdapter = class
    inherit DbDataAdapter
    interface IDataAdapter
    interface IDbDataAdapter
    interface ICloneable
Public NotInheritable Class OdbcDataAdapter
Inherits DbDataAdapter
Implements ICloneable
Public NotInheritable Class OdbcDataAdapter
Inherits DbDataAdapter
Inheritance
Implements

Examples

The following example uses OdbcCommand, OdbcDataAdapter, and OdbcConnection to select records and populate a DataSet with the selected rows.

public DataSet GetDataSetFromAdapter(
    DataSet dataSet, string connectionString, string queryString)
{
    using (OdbcConnection connection =
               new OdbcConnection(connectionString))
    {
        OdbcDataAdapter adapter =
            new OdbcDataAdapter(queryString, connection);

        // Open the connection and fill the DataSet.
        try
        {
            connection.Open();
            adapter.Fill(dataSet);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
    return dataSet;
}
Public Function GetDataSetFromAdapter( _
    ByVal dataSet As DataSet, ByVal connectionString As String, _
    ByVal queryString As String) As DataSet

    Using connection As New OdbcConnection(connectionString)
        Dim adapter As New OdbcDataAdapter(queryString, connection)

        ' Open the connection and fill the DataSet.
        Try
            connection.Open()
            adapter.Fill(dataSet)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using

    Return dataSet
End Function

Remarks

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

When the OdbcDataAdapter fills a DataSet, it creates the required 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 OdbcDataAdapter 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.

Note

When you call the Fill method on a data source that does not have a primary key column, the OdbcDataAdapter tries to promote the unique constraint column to the primary key. In the process, the OdbcDataAdapter marks the unique constraint as not nullable. This behavior works unless there is a null value in the unique constraint column. If there is a null value, the Fill method fails with a constraint violation. To avoid this situation, do not allow null values in the unique constraint column.

Note

Due to the limitations of native ODBC drivers, only one DataTable is ever returned when you call FillSchema. This is true even when executing SQL batch statements from which multiple DataTable objects would be expected.

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

Constructors

Name Description
OdbcDataAdapter()

Initializes a new instance of the OdbcDataAdapter class.

OdbcDataAdapter(OdbcCommand)

Initializes a new instance of the OdbcDataAdapter class with the specified SQL SELECT statement.

OdbcDataAdapter(String, OdbcConnection)

Initializes a new instance of the OdbcDataAdapter class with an SQL SELECT statement and an OdbcConnection.

OdbcDataAdapter(String, String)

Initializes a new instance of the OdbcDataAdapter class with an SQL SELECT statement and a connection string.

Properties

Name Description
DeleteCommand

Gets or sets an SQL statement or stored procedure used to delete records in the data source.

InsertCommand

Gets or sets an SQL statement or stored procedure used to insert new records into the data source.

SelectCommand

Gets or sets an SQL statement or stored procedure used to select records in the data source.

UpdateCommand

Gets or sets an SQL statement or stored procedure used to update records in the data source.

Events

Name Description
RowUpdated

Occurs during an update operation after a command is executed against the data source.

RowUpdating

Occurs during Update(DataSet) before a command is executed against the data source.

Explicit Interface Implementations

Name Description
ICloneable.Clone()

For a description of this member, see Clone().

IDbDataAdapter.DeleteCommand

For a description of this member, see DeleteCommand.

IDbDataAdapter.InsertCommand

For a description of this member, see InsertCommand.

IDbDataAdapter.SelectCommand

For a description of this member, see SelectCommand.

IDbDataAdapter.UpdateCommand

For a description of this member, see UpdateCommand.

Applies to

See also