Adding a data source question
-
I've searched the code project site, but I have been unable to find any articles that show how to add a system dsn through code. Could someone point me to a snippet of C++ that registers a DSN in the ODBC Data Sources? I appreciate any help you can give me. :) David Hisel -- http://www.hisel.com/
-
I've searched the code project site, but I have been unable to find any articles that show how to add a system dsn through code. Could someone point me to a snippet of C++ that registers a DSN in the ODBC Data Sources? I appreciate any help you can give me. :) David Hisel -- http://www.hisel.com/
I don't have any code, but I can tell you how I've done it. Its really pretty easy. You need to add some entries to the system registry. These entries define a DSN named APDB. [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\APDB] "Driver"="C:\\WINNT\\System32\\SQLSRV32.dll" "Server"="odyw2ksql02" "Database"="Odyssey_APDB" "LastUser"="obuser" [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources] "APDB"="SQL Server"
-
I don't have any code, but I can tell you how I've done it. Its really pretty easy. You need to add some entries to the system registry. These entries define a DSN named APDB. [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\APDB] "Driver"="C:\\WINNT\\System32\\SQLSRV32.dll" "Server"="odyw2ksql02" "Database"="Odyssey_APDB" "LastUser"="obuser" [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources] "APDB"="SQL Server"
Thanks! This is very helpful, I appreciate it. :) David Hisel -- http://www.hisel.com/
-
I've searched the code project site, but I have been unable to find any articles that show how to add a system dsn through code. Could someone point me to a snippet of C++ that registers a DSN in the ODBC Data Sources? I appreciate any help you can give me. :) David Hisel -- http://www.hisel.com/
Here is some code to do this:
BOOL CreateSource(LPCSTR lpszDriver, LPCSTR lpszAttributes, BOOL bSystem /*= TRUE*/)
{
//nahradit '$' nulou ('\0') v lpszAttributes
char attr[500];
char driver[250];if(lpszAttributes) { int mlen = strlen(lpszAttributes); strcpy(attr,lpszAttributes); char\* ch = attr; for (int i=0; i<mlen; i++,ch++) {if (\*ch == '$') \*ch = '\\0';} } else { attr\[0\] = 0; } //Pridani druhe nuly za retezec s driverem if(lpszDriver) { int mlen = strlen(lpszDriver); strcpy(driver,lpszDriver); strcat(driver,"\\0"); } else { driver\[0\] = 0; } WORD request; if(bSystem) request = ODBC\_ADD\_SYS\_DSN; else request = ODBC\_ADD\_DSN; //zavolani ODBC API return SQLConfigDataSource(NULL,request,driver,attr);
}
BOOL CreateAccessSource(LPCSTR lpszName, LPCSTR lpszPath, LPCSTR lpszDescription /*= NULL*/, BOOL bSystem /*= TRUE*/)
{
char* szDesc;
szDesc=new char[500];if(lpszDescription) sprintf(szDesc,"DSN=%s$ DESCRIPTION=%s$ DBQ=%s$ FIL=MicrosoftAccess$ DEFAULTDIR=C:\\\\$$",lpszName,lpszDescription,lpszPath); else sprintf(szDesc,"DSN=%s$ DBQ=%s$ FIL=MicrosoftAccess$ DEFAULTDIR=C:\\\\$$",lpszName,lpszPath); return CreateSource("Microsoft Access Driver (\*.mdb)",(LPCSTR)szDesc,bSystem);
}
Pavel Sonork 100.15206
-
Here is some code to do this:
BOOL CreateSource(LPCSTR lpszDriver, LPCSTR lpszAttributes, BOOL bSystem /*= TRUE*/)
{
//nahradit '$' nulou ('\0') v lpszAttributes
char attr[500];
char driver[250];if(lpszAttributes) { int mlen = strlen(lpszAttributes); strcpy(attr,lpszAttributes); char\* ch = attr; for (int i=0; i<mlen; i++,ch++) {if (\*ch == '$') \*ch = '\\0';} } else { attr\[0\] = 0; } //Pridani druhe nuly za retezec s driverem if(lpszDriver) { int mlen = strlen(lpszDriver); strcpy(driver,lpszDriver); strcat(driver,"\\0"); } else { driver\[0\] = 0; } WORD request; if(bSystem) request = ODBC\_ADD\_SYS\_DSN; else request = ODBC\_ADD\_DSN; //zavolani ODBC API return SQLConfigDataSource(NULL,request,driver,attr);
}
BOOL CreateAccessSource(LPCSTR lpszName, LPCSTR lpszPath, LPCSTR lpszDescription /*= NULL*/, BOOL bSystem /*= TRUE*/)
{
char* szDesc;
szDesc=new char[500];if(lpszDescription) sprintf(szDesc,"DSN=%s$ DESCRIPTION=%s$ DBQ=%s$ FIL=MicrosoftAccess$ DEFAULTDIR=C:\\\\$$",lpszName,lpszDescription,lpszPath); else sprintf(szDesc,"DSN=%s$ DBQ=%s$ FIL=MicrosoftAccess$ DEFAULTDIR=C:\\\\$$",lpszName,lpszPath); return CreateSource("Microsoft Access Driver (\*.mdb)",(LPCSTR)szDesc,bSystem);
}
Pavel Sonork 100.15206
This is great! Thank you. Note: your code was mis-interpreted as HTML inside the first if block, look at the for loop to see what happened. Could you send this via email to david at hisel dot com? Thanks again, David Hisel -- http://www.hisel.com/
-
This is great! Thank you. Note: your code was mis-interpreted as HTML inside the first if block, look at the for loop to see what happened. Could you send this via email to david at hisel dot com? Thanks again, David Hisel -- http://www.hisel.com/
I fixed it. Pavel Sonork 100.15206