Tải bản đầy đủ - 0 (trang)
6-12. Importing Data over ODBC

6-12. Importing Data over ODBC

Tải bản đầy đủ - 0trang

Chapter 6 ■ Miscellaneous Data Sources



Figure 6-31.  Adding an ODBC connection manager

3.



Click Add.



4.



Click New. You then must either select an existing DSN from those available (as

described in the following mini-recipe) or select Use Connection String and enter the

appropriate ODBC connection string for DSN-less connectivity.



5.



Click OK to complete the configuration.



Generating an ODBC connection string is described in the last of the mini-recipes in this recipe.



331

www.it-ebooks.info



Chapter 6 ■ Miscellaneous Data Sources



Configuring an ODBC DSN

The standard way to configure an ODBC DSN is like this:

1.



Click Start menu ➤ Control Panel ➤ Administrative Tools ➤ Data Sources (ODBC).

You might need to give Windows permission to run it!



2.



Select System DSN. The ODBC Data Source Administrator dialog box should look like

Figure 6-32.



Figure 6-32.  The ODBC Data Source Administrator

3.



Click Add. Select the data source to which you want to connect from those available

(and by definition installed on your server). You should see the Create New Data

Source dialog box, as shown in Figure 6-33.



332

www.it-ebooks.info



Chapter 6 ■ Miscellaneous Data Sources



Figure 6-33.  Creating a new ODBC data source

4.



Select the driver and click Finish. You should then see the dialog box specific to the

ODBC driver that you have selected, which you must now configure.



5.



Create or open an SSIS package.



6.



Add a new connection manager (at package level, if you prefer—and if you are using

SSIS 2012).



7.



For SSIS 2012, select the ODBC Connection Manager and select the DSN that you

created. (For SSIS 2005 and 2008, select an ADO.NET connection and use the ODBC

data provider. Then select the DSN that you created.)



You can then use the ODBC source in an SSIS Data Flow process, or when using OPENROWSET, or in a linked

server connection. I cannot give examples of these because I am trying to remain as resolutely generic as

possible. So you have to consult the documentation for your specific ODBC driver for examples of how to use

these connectivity methods.



File DSNs

You can also use the ODBC Administrator tool to create file DSNs. The technique is virtually identical to that just

described, except that after selecting the ODBC driver, you have to click Next and enter a file name for the DSN

(and potentially choose a destination directory) before clicking Finish.

A file DSN is simply a text file that could be something like the following

(C:\SQL2012DIRecipes\CH06\File.dsn):

[ODBC]

DRIVER = SQL Server Native Client 10.0

UID = Adam



333

www.it-ebooks.info



Chapter 6 ■ Miscellaneous Data Sources



DATABASE = CarSales

WSID = ADAM02

APP = Microsoft® Windows® Operating System

Trusted_Connection = Yes

SERVER = ADAM02

File DSNs can be used by SSIS to create connection strings for ODBC connection managers (in SSIS 2012) or

for ODBC over ADO.NET connection managers (in SSIS 2005/2008), as is shown next.



File DSNs in SSIS 2012

File DSNs can also be used to configure the connection string for ODBC connections in SSIS 2012.

1.



Follow steps 1 to 3 in the ODBC in SSIS 2012 mini-recipe above.



2.



Click New.



3.



Click Use Connection String.



4.



Click Build.



5.



Browse to the directory where you saved the file DSN.



6.



Click OK.



7.



Finalize any parameters that are required but not supplied by the DSN.



8.



Click OK twice.



The connection string will be added to the dialog box.



ODBC and SSIS 2005/2008

The “older” versions of SSIS require you to use ODBC over ADO.NET as your System and User DSNs.

1.



In a new or existing SSIS package, right-click in the Connection Managers tab and

select New ADO.NET Connection.



2.



Click New.



3.



Click the pop-up list of providers. You should see something like Figure 6-34.



334

www.it-ebooks.info



Chapter 6 ■ Miscellaneous Data Sources



Figure 6-34.  .NET data providers

4.



Select the ODBC data provider.



5.



Click OK.



6.



Ensure that the “Use user or system data source name” radio button is selected.

Choose an existing DSN from those available when you expand the list of DSNs. You

should see a dialog box like Figure 6-35.



335

www.it-ebooks.info



Chapter 6 ■ Miscellaneous Data Sources



Figure 6-35.  Selecting a DSN

7.



Enter the username and password, if required.



8.



Click OK.



File DSNs in SSIS 2005/2008

File DSNs can also be used to configure SSIS ODBC connection managers. It is worth noting nonetheless that the

configuration information in the file DSN is used as a source, and that no link to the file is maintained, as all the

configuration information is stored in SSIS.

1.



Follow steps 1 to 5 in the “ODBC in SSIS 2005/2008” mini-recipe.



2.



Ensure that the Use Connection String radio button is selected. Click Build.



336

www.it-ebooks.info



Chapter 6 ■ Miscellaneous Data Sources



3.



Browse to the file DSN that you created—or were given.



4.



Once the file DSN is selected, click OK.



5.



Provide any required security/logon information. The dialog box should look like

Figure 6-31.



6.



Test the connection. Click OK.



“DSN-less” ODBC Connections

You can make SSIS use an ODBC driver without needing an existing DSN. This works for all versions of SSIS. It

creates a .dsn file on disk in any case.

1.



Using the preceding recipe(s) for file DSNs, display the Connection Manager dialog

box shown in Figure 6-36.



Figure 6-36.  The ODBC connection manager dialog box



2.



Ensure that the Use Connection String radio button is selected. Click Build.



3.



Click New. Select the ODBC driver corresponding to the data source that you wish

to use.



4.



Click Next.



5.



Save the File DSN. You may decide on the file name and directory—but the .dsn

extension is added automatically.



6.



Click Next.



337

www.it-ebooks.info



Chapter 6 ■ Miscellaneous Data Sources



7.



Click Finish.



8.



Configure the ODBC driver.



9.



Click OK.



Exporting and Importing System and User DSNs

System and User DSNs are stored in the registry. While this makes them easy to tweak using the ODBC

Administrator tool, it makes deployment a little more complex.

There is a particular registry entry:—HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI—that contains a

complete list of the System DSN sources defined on your machine. You can use regedit (or a similar utility) to

export your registry entries, which can then be copied as required. This avoids regenerating DSNs manually.

It can be done as follows:

1.



Open the registry editor.



2.



Navigate to the ODBC folder (under HKEY_LOCAL_MACHINE > SOFTWARE).



3.



Export a .reg file with a suitable name.



4.



Import the .reg file into the registry of a destination server using regedit.exe—or by

double-clicking the .reg file.



5.



Reboot the destination server.



User DSNs are found in HKEY_CURRENT_USER\Software\ODBC\ODBC.INI.

Assuming that you have created a System or User DSN, it is worth looking at how these may be used by SSIS.



■■Note  Be extremely careful when using a Registry editor and ensure that you do not make any unwanted or

dangerous changes to the registry.



How It Works

Well, finally, you have tried everything else every other way, and you have come to the conclusion that ODBC is

the only remaining solution. Yes, it can be slow (at least when run over an ADO.NET provider), and, yes, it can be

clumsy with some data types, but when all is said and done, it may be the only way to go. Intriguingly, Microsoft

has indicated that ODBC is the future, so the time when this type of connection was to be relegated to “outsider”

status is clearly past.

As specific ODBC connections are described in detail for MySQL and other relational sources, this recipe

is a succinct generic overview of how to create and use ODBC connection managers. As each ODBC driver has

its own specific configuration options, you have to know how to set up each one; so be prepared to dive into

the source documentation and any other useful reference information that you can find. This means that out of

necessity I must remain very generic when explaining the configuration techniques for ODBC drivers.

I imagine that most times you will create a DSN using the ODBC Administrator tool. On a 32-bit or a

64-bit system, this is found in %SystemRoot%\system32\odbcad32.exe. Admittedly, this is a surprising

naming convention. Should you need the 32-bit version on a 64-bit machine, you need to look for

%SystemRoot%\Windows\SysWOW64\odbcad32.exe.



338

www.it-ebooks.info



Chapter 6 ■ Miscellaneous Data Sources



I have a weakness for creating System DSNs rather than User DSNs. The reasons for this are as follows:





Because they are not specific to the user who is running the process, it is one less

potential source of failure.







Possibly more important, because there is no registry redirection for User DSNs, and

the ODBC Administrator tool displays both 32-bit and 64-bit DSNs, you can end up

attempting to use a DSN that cannot function in the environment that you are using.



It is also worth noting that a 64-bit server running odbcad32.exe from the Control Panel will run the 64-bit

version of the ODBC connection manager.



Hints, Tips, and Traps





If you create a connection string–based DSN in SSIS, the ODBC Administrator will create

a .dsn file on disk in any case. You have to delete this file manually if you do not want it

stored on disk.







The ODBC Administrator displays User DSNs for both 32- and 64-bit drivers. This does

not mean that a 64-bit application uses the 32-bit driver, however, because registry

redirection is not available for User DSNs.







Redirection is available for System DSNs. Consequently, System DSNs for 32-bit drivers

and 64-bit drivers are kept separate. The 64-bit ODBC Administrator does not display

System DSNs that are created by the 32-bit ODBC Administrator. Similarly, the 32-bit

ODBC Administrator does not display System DSNs that are created by the 64-bit ODBC

Administrator.







Consequently, on 64-bit systems, you are best advised to install both 32-bit and 64-bit

versions of drivers where these are available.







In SSIS 2005 and 2008, don’t try to set up an ODBC connection manager by right-clicking

in the Connection Manager pane, and then selecting New Connection, followed by

ODBC. This will not work because it is not a “managed” connection!



6-13. Linking to 32-bit data sources from a 64-bit SQL Server

Problem

You are working in a 64-bit environment and need to implement a linked server to data for which only a 32-bit

provider is available.



Solution

Install a second 32-bit instance of SQL Server and set up a (32-bit) linked server to your source data. You can then

link the 64-bit version of SQL Server to this. Although painful to carry out and slow to load data, this is how it can

be done (here using Access as a sample 32-bit data source).

1.



Install a second 32-bit instance of SQL Server.



2.



Configure both servers to allow ad hoc distributed queries. This is described in

Recipe 1-4.



339

www.it-ebooks.info



Chapter 6 ■ MisCellaneous Data sourCes



3.



Select—or better still, create—a database on the 32-bit SQL Server instance that will

be used to route the queries from the linked Access database to the 64-bit instance.

I will name the database CarSales32.



4.



On the 32-bit SQL Server instance, create a linked server to connect to the Access

database. The following code snippet can do this. Of course, this is where you will

have to configure the source data which you are using to be as a linked server:

EXECUTE master.dbo.sp_addlinkedserver @server = N'Access', @srvproduct = N'Access',

@provider = N'Microsoft.Jet.OLEDB.4.0', @datasrc = C:\SQL2012DIRecipes\CH01\CarSales.mdb';



5.



On the 32-bit SQL Server instance, in the database that you created in step 3, create a

view to query the source data database. This could be as simple as

(C:\SQL2012DIRecipes\CH01\vw_SQL32.Sql):

CREATE VIEW vw_SQL32

AS

SELECT theText from Access. . .Client;

GO



6.



On the SQL Server 64-bit instance, create a linked server to the 32-bit SQL Server

instance.

EXECUTE master.dbo.sp_addlinkedserver @server = 'ADAM32', @srvproduct = '', 

@provider = 'SQLNCLI', @datasrc = 'ADAM02\SQL32Bit';



7.



From the 64-bit instance, read the data from the 32-bit linked server:

SELECT ID, Marque FROM ADAM32.CarSales32.dbo.vw_SQL32;



How It Works

So, what can you do if you are working in a 64-bit environment and need to implement a 32-bit linked server

when only a 32-bit driver (OLEDB or ODBC) exists? The approach described here is a possible workaround,

although it is a lot of initial effort.

What you did was:





Install a second 32-bit SQL Server instance.







Link the source data to the 32-bit SQL Server instance.







Link this second instance to your 64-bit instance.



I agree that this is a lot of effort to achieve something that should be simple; but in some cases, you may find

that you have no alternative.



Hints, Tips, and Traps





You can also link to 32-bit Excel in a 64-bit environment this way.







I am assuming that you have full admin rights to both servers. If this is not the case, you

have to map local and remote logins appropriately.



340

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

6-12. Importing Data over ODBC

Tải bản đầy đủ ngay(0 tr)

×