DSN Creation  
Author Message
Harley Ramos





PostPosted: Visual FoxPro General, DSN Creation Top


Hello I need to create a System DSN ODBC for SQL. I have seen a lot of solutions using DSN-less solutions but that does not work for me.

I have to use this DSN from a third-party application where I don't have its source code. What I want with this simple code is to execute it before the third-party application, just to change some ODBC attributes (using the same DSN name) like Server same, user, password, etc...

I have been able to delete the DSN, but no Create or Modify it.

I am using SQLConfigDataSource function in this way:

#define ODBC_ADD_DSN 1 &&' Add data source
#define ODBC_ADD_SYS_DSN 4 &&' Add data source
#define ODBC_CONFIG_SYS_DSN 5 &&' Configure (edit) data source
#define ODBC_REMOVE_SYS_DSN 6 &&' Remove data source
#define vbAPINull 0 &&' NULL Pointer

DECLARE LONG SQLConfigDataSource IN ODBCCP32.DLL ;
LONG hwndParent, LONG fRequest, STRING lpszDriver, STRING lpszAttributes

strDriver = "SQL Server"
strAttributes = "SERVER=LOCALPC" + Chr(0)
strAttributes = strAttributes + "DESCRIPTION=test" + Chr(0)
strAttributes = strAttributes + "DSN=local_test" + Chr(0)
strAttributes = strAttributes + "DATABASE=master" + Chr(0)
strAttributes = strAttributes + "UID=sa" + Chr(0)
strAttributes = strAttributes + "PWD=temp0" + Chr(0)

intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, strDriver, strAttributes)
If intRet>0
=MessageBox( "DSN Created" )
Else
=MessageBox( "Create Failed" )
EndIf

All the time I got the message "Create Failed". Why this could be happening



Visual FoxPro1  
 
 
AndyKr





PostPosted: Visual FoxPro General, DSN Creation Top

Not sure about this specific question, but System DSN information is stored in the Registry under the following keys:

HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI

and the drivers are stored under:

HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI

Also the key "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" is the list of DSNs used to populate the windows ODBC Connection tool

If you have the DSN, you can simply modify the settings in the registry (programmatically) rather than deleting and re-creating it. (It may be that you don't have rights to do that)