Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
This Document is not yet Rated  Rate this Document

Programmatically Creating and Registering a DSN in C++

Primary Software: LabWindows/CVI Add-ons>>Enterprise Connectivity Toolset
Primary Software Version: 1.1
Primary Software Fixed Version: N/A
Secondary Software: N/A

Problem:
I want to create and register a DSN programmatically in C++ without using the administrator program. How can I do this?

Solution:
Creating and Registering Data Source Names (DSN) in C++ is accomplished by using the Windows SDK function call SQLConfigDataSource. This function processes requests based on the attributes passed into the function. Valid requests include allowing the user to add, remove, and configure DSNs. If a user wishes to add a new DSN and to register the DSN in the ODBC.ini file and the registry, two separate calls must be made to SQLConfigDataSource.
  1. The first function call must use the ODBC_ADD_DSN request type and pass CREATE_DB=TESTDSN_DB.mdb\00 as an attribute. This function call will create the MS Access file but will not register the file. This is exhibited in the code fragment below:

    ReturnResult = SQLConfigDataSource (NULL, ODBC_CONFIG_SYS_DSN, "Microsoft Access Driver (*.mdb)", "DSN=TESTDSN_DSN\0CREATE_DB=c:\\TESTDSN_DB.mdb\00")

  2. To register the DSN so that ODBC servers will know where to locate the file, a second call to SQLConfigDataSource must used. On the second call use ODBC_ADD_SYS_DSN for the request type and pass the location of the MS Access file. The function call below yields a system DSN and expects a file named c:\TESTDSN_DB.mdb to exist. There are correct entries not only in the ODBC.ini file but in the ODBC section of register. You can view the parameters via the ODBC Administrator tool or Start»Run and type regedit.

    ReturnResult = SQLConfigDataSource (NULL, ODBC_ADD_SYS_DSN, "MicrosoftAccess Driver (*.mdb)", "DSN=TESTDSN_DSN\00DBQ=c:\\TESTDSN_DB.mdb\ 00FIL=MSAccess\00Description=TESTDSN_database\00UID=\00");

Making these two function calls will allow you to programmatically create and register your DSN. This can be especially useful on distributed applications where you do not want to require user input. For help and additional documentation on this function call please see the Microsoft link below.

Related Links:
External Site: Microsoft ODBC Programmer's Reference
KnowledgeBase 2E7GIDDT: What Is the Difference between a System DSN, a User DSN, a File DSN, Universal Data Link (UDL), and DSN-less Connection?
KnowledgeBase 3OSAJHTJ: Programmatically Creating Universal Data Link (*.udl / UDL) Files
KnowledgeBase 2U2FII5A: Creating a DSN File To Communicate with Access Database in LabVIEW?

Attachments:





Report Date: 12/30/2002
Last Updated: 12/31/2008
Document ID: 2ST89LJY

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit