
SQLConfigDataSource Example
The following example uses the ::SQLConfigDataSource ODBC API function to create a new Excel data source called New Excel Data Source:
|
SQLConfigDataSource(NULL,ODBC_ADD_DSN, "Excel Files (*.xls)",
"DSN=New Excel Data Source\0"
"Description=New Excel Data Source\0"
"FileType=Excel\0"
"DataDirectory=C:\\EXCELDIR\0"
"MaxScanRows=20\0"); |
Note that the data source is actually a directory (C:\EXCELDIR); this directory must exist. The Excel driver uses directories as its data sources and files as the individual tables (one table per .xls file).
For more information about creating tables, see Data Source: Programmatically Creating a Table in an ODBC Data Source.
The following information discusses the parameters that need to be passed to the ::SQLConfigDataSource ODBC API function. To use ::SQLConfigDataSource, you must include the Odbcinst.h header file and use the Odbcinst.lib import library. Also, Odbccp32.dll must be in the path at run time (or Odbcinst.dll for 16 bit).
You can create an ODBC data source name using ODBC Administrator or a similar utility. However, sometimes it is desirable to create a data source name directly from your application to obtain access without requiring the user to run a separate utility.
ODBC Administrator (typically installed in Control Panel) creates a new data source by putting entries in the Windows registry (or, for 16 bit, in the Odbc.ini file). The ODBC Driver Manager queries this file to obtain the required information about the data source. It is important to know what information needs to be placed in the registry because you need to supply it with the call to ::SQLConfigDataSource.
Although this information could be written directly to the registry without using ::SQLConfigDataSource, any application that does so is relying on the current technique that the Driver Manager uses to maintain its data. If a later revision to the ODBC Driver Manager implements record keeping about data sources in a different way, any application that uses this technique is broken. It is generally advisable to use an API function when one is provided. For example, your code is portable from 16 bit to 32 bit if you use the ::SQLConfigDataSource function, because the function correctly writes to the Odbc.ini file or to the registry.