Configuring an OLEDB Connection String
The .NET Framework
data provider for OLE DB connects to an OLE DB data sources through the OleDbConnection
object. The OLE DB provider connection string is specified using the ConnectionString
property of the OleDbConnection
object. This property specifies all settings needed to establish the connection
to the data source and matches the OLE DB connection string format with an
added Provider
key-value pair specifying the OLE DB provider is required.
OLEDB Connection String Keywords :
- Data Source : The name of the database or physical location of the database file.
- File Name : The physical
location of a file that contains the real connection string.
- Persist Security Info: If set to true, retrieving the connection string returns the complete connection
string that was originally provided. If set to false, the connection
string will contain the information that was originally provided, minus the
security information.
- Provider: The
vendor-specific driver to use for connecting to the data store.
Following example shows how to access a data source using an OLE DB provider
using System; using System.Data.OleDb;
namespace OleDbConncetionString { class Program { static void Main(string[] args) { string oledbConnectString = "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI";
string sqlSelect = "SELECT TOP 4 ProductID, Name, ProductNumber FROM Production.Product";
// Create an OleDb Connection using (OleDbConnection connection = new OleDbConnection(oledbConnectString)) { OleDbCommand command = new OleDbCommand(sqlSelect, connection);
// Execute the DataReader connection.Open(); OleDbDataReader reader = command.ExecuteReader();
// Output the data from the DataReader to the console Console.WriteLine("ProductID\tProductName\t\tProductNumber\n"); while (reader.Read()) Console.WriteLine("{0}\t\t{1}\t\t{2}", reader[0], reader[1], reader[2]); }
Console.WriteLine("\nPress any key to continue."); Console.ReadLine(); } } }
|
Output :
The
following providers have been tested with ADO.NET.
Driver
|
Provider
|
SQLOLEDB
|
Microsoft
OLE DB provider for SQL Server
|
MSDAORA
|
Microsoft
OLE DB provider for Oracle
|
Microsoft.Jet.OLEDB.4.0
|
OLE
DB provider for Microsoft Jet
|