OLE DB
The OLE DB .NET Provider (System.Data.OleDb) enables applications to access data sources through OLE DB. It provides classes to connect, execute commands, retrieve results, and manage parameters.
Namespaces
Namespace | Description |
---|---|
System.Data.OleDb | Contains the OLE DB data provider classes. |
Key Classes
- OleDbConnection – Represents an open connection to a data source.
- OleDbCommand – Executes a command against a data source.
- OleDbDataReader – Provides forward-only, read-only access to query results.
- OleDbParameter – Represents a parameter to an OleDbCommand.
- OleDbException – Represents an OLE DB error.
Getting Started
The following example demonstrates connecting to an Access database, executing a query, and reading results.
using System;
using System.Data.OleDb;
class Program
{
static void Main()
{
string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Example.accdb;";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
string sql = "SELECT Id, Name FROM Employees WHERE Department = ?";
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
cmd.Parameters.Add(new OleDbParameter("@dept", "Sales"));
using (OleDbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"{reader["Id"]}: {reader["Name"]}");
}
}
}
}
}
}