Connecting to an OLE DB Data Source

348 Manager, specifying that the account will use Windows Authentication and supplying the Windows user or group that is to be given access. 2. The network administrator assigns rights to this login account as appropriate. 3. The data access code indicates in the connection string that Integrated Windows Security should be used, as shown here: Data Source= SomeMachine; Initial Catalog=Northwind; Integrated Security=True When using Integrated Windows Authentication, it is necessary to know what Windows login account a process will run under and to set up appropriate rights for that login account in SQL Server Enterprise Manager. A program running on a local machine generally runs under the login account of the user that started the program. A component running in Microsoft Transaction Server MTS or COM+ runs under a login account specified in the MTS or COM+ Explorer. Code that is embedded in an ASP.NET web page runs under a login account specified in Internet Information Server IIS. Consult the documentation for these products for information on specifying the login account under which components run. Consult the SQL Server Books Online for information on setting up SQL Server login accounts and on specifying account privileges.

8.4 Connecting to an OLE DB Data Source

OLE DB is a specification for wrapping data sources in a COM-based API so that data sources can be accessed in a polymorphic way. The concept is the same as ADO.NETs concept of managed providers. OLE DB predates ADO.NET and will eventually be superseded by it. However, over the years, OLE DB providers have been written for many data sources, including Oracle, Microsoft Access, Microsoft Exchange, and others, whereas currently only one product—SQL Server—is natively supported by an ADO.NET managed provider. To provide immediate support in ADO.NET for a wide range of data sources, Microsoft has supplied an ADO.NET managed provider for OLE DB. That means that ADO.NET can work with any data source for which there is an OLE DB data provider. Furthermore, because there is an OLE DB provider that wraps ODBC an even older data-access technology, ADO.NET can work with virtually all legacy data, regardless of the source. Connecting to an OLE DB data source is similar to connecting to SQL Server, with a few differences: the OleDbConnection class from the System.Data.OleDb namespace is used instead of the SqlConnection class, and the connection string is slightly different. When using the OleDbConnection class, the connection string must specify the OLE DB provider that is to be used as well as additional information that tells the OLE DB provider where the actual data is. For example, the following code opens a connection to the Northwind sample database in Microsoft Access: Open a connection to the database. Dim strConnection As String = _ Provider=Microsoft.Jet.OLEDB.4.0;Data Source= _ C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb Dim cn As OleDbConnection = New OleDbConnectionstrConnection cn.Open Similarly, this code opens a connection to an Oracle database: Open a connection to the database. Dim strConnection As String = _ 349 Provider=MSDAORA.1;User ID=MyID;Password=MyPassword; _ Data Source=MyDatabaseService.MyDomain.com Dim cn As OleDbConnection = New OleDbConnectionstrConnection cn.Open The values of each setting in the connection string, and even the set of settings that are allowed in the connection string, are dependent on the specific OLE DB provider being used. Refer to the documentation for the specific OLE DB provider for more information. Table 8- 2 shows the provider names for several of the most common OLE DB providers. Table 8-2. Common OLE DB provider names Data source OLE DB provider name Microsoft Access Microsoft.Jet.OLEDB.4.0 Microsoft Indexing Service MSIDXS.1 Microsoft SQL Server SQLOLEDB.1 Oracle MSDAORA.1

8.5 Reading Data into a DataSet