Programmatically Creating and Registering a DSN in C++ Primary Software: LabWindows/CVI Add-ons>>Enterprise Connectivity ToolsetPrimary 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. The SQLConfigDataSource 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 Register the DSN in the ODBC.ini file and the registry, two separate calls must be made to SQLConfigDataSource. 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 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")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: Microsoft ODBC Programmer's Reference Attachments:
Report Date: 12/30/2002 Last Updated: 12/05/2004 Document ID: 2ST89LJY |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
