March 2008
Intermediate to advanced
982 pages
22h 43m
English
You want to access your data source using an OLE DB provider from your .NET application.
Use the OLE DB .NET data provider to access data exposed through an OLE DB driver.
The solution creates an OleDbDataReader and uses it to output the FirstName and LastName columns from the first five rows in the Person.Contact table in the AdventureWorks sample database.
The C# code in Program.cs in the project ConnectOleDbDataSource is shown in Example 1-9.
Example 1-9. File: Program.cs for ConnectOleDbDataSource solution
using System;
using System.Data.OleDb;
namespace ConnectOleDbDataSource
{
class Program
{
static void Main(string[] args)
{
string oledbConnectString = "Provider=SQLOLEDB;Data Source=(local);" +
"Initial Catalog=AdventureWorks;Integrated Security=SSPI";
string sqlSelect = "SELECT TOP 5 Title, FirstName, LastName " +
"FROM Person.Contact";
// Create an ODBC 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
while (reader.Read( ))
Console.WriteLine("{0} {1}", reader[0], reader[1]);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey( );
}
}
}The output is shown in Figure 1-8.
Figure 1-8. Output for ConnectOleDbDataSource solution
An OLE DB provider is ...
Read now
Unlock full access