ODBCDataAdapter

Class ODBCDataAdapter

Represents a data adapter that bridges the DataSet class and data sources for Odbc data.

Class Information

Namespace: System.Data.Odbc
Assembly: System.Data.Odbc.dll

Inheritance

Object
   ↳ MarshalByRefObject
      ↳ DbDataAdapter
         ↳ ODBCDataAdapter

Remarks

The ODBCDataAdapter is used to retrieve records from an Odbc data source and populate the DataSet with rows, and it also supports filling the DataSet and reflecting changes made to the DataSet back to the data source.

To use the ODBCDataAdapter, you must specify a valid Odbc connection string and a SQL statement or stored procedure that will be executed against the data source.

Public Constructors

Public Properties

Public Methods

Protected Methods

Remarks

The ODBCDataAdapter class provides a way to interact with ODBC data sources. It can be used to fetch data from a database into a DataSet, and then to send any changes made to the DataSet back to the database.

When you create an instance of ODBCDataAdapter, you typically specify the SQL query to retrieve data (using the SelectCommand property) and optionally commands for inserting, updating, and deleting data.

The Fill() method populates a DataSet with the results of the SelectCommand. If the DataSet already contains tables with the same names as the tables returned by the query, the new rows are appended to the existing tables.

The Update() method processes changes made to the rows within the DataSet and applies them to the data source using the InsertCommand, UpdateCommand, and DeleteCommand properties. The adapter determines which command to execute for each modified row based on its RowState.

The AcceptChangesDuringFill property controls whether the AcceptChanges() method is automatically called on the DataSet after the Fill() operation. If true, changes are accepted, and rows are marked as unchanged. If false, changes remain, and rows are marked as modified.

Example

The following example demonstrates how to use the ODBCDataAdapter to retrieve data from an ODBC data source and populate a DataSet.

using System;
using System.Data;
using System.Data.Odbc;

public class OdbcDataAdapterExample
{
    public static void Main(string[] args)
    {
        string connectionString = "DSN=MyOdbcDataSource;Uid=myUser;Pwd=myPassword;";
        string selectQuery = "SELECT CustomerID, CompanyName FROM Customers";

        using (OdbcConnection connection = new OdbcConnection(connectionString))
        {
            try
            {
                connection.Open();
                Console.WriteLine("Connection opened successfully.");

                using (OdbcDataAdapter adapter = new OdbcDataAdapter(selectQuery, connection))
                {
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet, "Customers");

                    Console.WriteLine("\nCustomer Data:");
                    foreach (DataRow row in dataSet.Tables["Customers"].Rows)
                    {
                        Console.WriteLine($"ID: {row["CustomerID"]}, Name: {row["CompanyName"]}");
                    }
                }
            }
            catch (OdbcException ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An unexpected error occurred: {ex.Message}");
            }
        }
    }
}