LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Create ODBC Database DSN Dynamically

I need to create and ODBC Database DSN dynamically as I cannot count on the end user knowing how to do so correctly on the windows XP operating system. I've google searched and seen solutions for this in java, C#, C++, and other languages. Most of these solutions rely on a high level library function in that language to create the DSN.

I think that I can do this by using the LabWindows/CVI registry functions in the Programmer's Toolkit, but am unsure on exactly what and where to write this information.

Has anyone done this? Any examples of this in "C"?

Doug Wendelboe
Noninvasive Medical Technologies, LLC
Las Vegas, NV
0 Kudos
Message 1 of 13
(9,196 Views)
Doug,

You can dynamically create a DSN by adding registry values to your windows registry. To write to the windows registry you would use functions from the 'Windows Registry' function set in the Programmer's Toolbox. The values that you will need to write to the registry depend on what type of database that you wish to use. There is only one common registry value you need to create no matter what kind of database you are using. This value is the DSN of the database in the following registry folder:
"HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources\"

You need to put the name of the DSN entry as a value in this folder. For instance if I were creating a DSN 'SQLDatabase' that uses SQL Server I would enter the following value in the "HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources\" directory:

Name - SQLDatabase (DSN)
Type - REG_SZ (Type of regisrty entry)
Data - SQL Server (ODBC Source Driver)

Once that is done, you will need to create a new key in the "HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources\" folder that has the name of the new DSN entry. In our example case I would create a "HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources\SQLDatabase" key.

After creating the "HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources\SQLDatabase" key I would need to enter the appropriate values in the newly created key.

Some Example Code:

/*Create the inital DSN entry*/
RegWriteString (REGKEY_HKLM, "Software\ODBC\ODBC.INI\ODBC Data Sources", "SQLDatabase", "SQL Server");

/*The DSN needs to know the logical name of the SQL Server database we wish to use, so here we create that name. At the same time we also create the 'SQLDatabase' registry key*/
RegWriteString (REGKEY_HKLM, "Software\ODBC\ODBC.INI\ODBC Data Sources\SQLDatabase", "Database", "SQLDatabase");

If you have a specific type of database you wish to use, I can post more specific code, but as I said it will vary depending on the type of database that needs to be used. I would suggest creating the DSN that you want by using the Windows 'Data Sources' Utility and then using regedit to go to the "HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources" registry entry. You will be able to see what the template for the DSN you need to build on the fly should look like.
Message 2 of 13
(9,186 Views)
Thanks, Dave, for your detailed posting. The database that I am currently using for this application is MS Access. I'll try plugging in my values into your code and see if I can get it to initialize.
0 Kudos
Message 3 of 13
(9,182 Views)
Dave,

Something like this for MS Access databases?

RegWriteString (REGKEY_HKLM,
"Software\ODBC\ODBC.INI\ODBC Data Sources",
"IQ3_Database", // the DSN
"MS Access"); // the ODBC Source Driver

RegWriteString (REGKEY_HKLM,
"Software\ODBC\ODBC.INI\ODBC Data Sources\IQ3_Database",
"Microsoft Access Driver (*.mdb)",
"IQ3_Database");

Does the driver in the second write registry require the exact name as seen, including i.e., "(*.mdb)"
0 Kudos
Message 4 of 13
(9,177 Views)
Doug,

Check out the code snippit that I have attached, it should create a DSN for a MS Access database.
0 Kudos
Message 5 of 13
(9,170 Views)
That worked quite well, Dave.

I added the DBQ entery to point to the actual database. In addition, I added another routine to read read the DNS name on program startup, and if it does not exist, then create it. Tried it on an uninitialized machine and it created it and worked well.

Thanks again for the detailed code. If you're ever in Las Vegas I'll buy you a lunch 🙂
0 Kudos
Message 6 of 13
(9,164 Views)

Hi,

I am using sql server 2000 and labview . In my program i want to create DSN dynamically. Please send me the steps that how to create DSN Clearly..

Thanks in Advance.

Vaira Muthu

0 Kudos
Message 7 of 13
(8,498 Views)

Hi Vaira,

You may try to create new registry keys similar to the suggestion Doug and Dave mentioned above. You will simply make use of the Windows Registry Access VIs located in the Connectivity library. 
You may however take another route and simply enable the prompt option in the DB Tools Open Connection.vi to create a DNS on the fly.

Nestor
0 Kudos
Message 8 of 13
(8,450 Views)


@vaira muthu wrote:

Hi,

I am using sql server 2000 and labview . In my program i want to create DSN dynamically. Please send me the steps that how to create DSN Clearly..

Thanks in Advance.

Vaira Muthu




Use the ODBC Manager to configure such a DSN and then check the registry for the created keys and folders and write a routine that creates/updates those keys in the registry similar to what has been shown already for SQL Server and MS Access.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 9 of 13
(8,443 Views)

Hello all,

I have browsed through the sample code - DSNCreation.c - that was attached code. However, it seems the code would not work because the code does not contain the location of where to find the  " *.mdb"   file that would be used. How do you implement this dynamically?

Thanks,

Robert

 

 

0 Kudos
Message 10 of 13
(8,361 Views)