something like
Code:
iRet=SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN, (LPSTR) "SQL Server",
(LPSTR)"DSN=TestingMyDSN\0"
"Server=192.168.0.1\0"
"Database=MY_DB_NAME\0"
"Trusted_Connection=NO\0"
"Description=This is the DSN for my app\0");
if(!iRet)
{
//error check
iRet=GetLastError();
//remove DSN when finished?
SQLConfigDataSource(NULL,ODBC_REMOVE_SYS_DSN,NULL,(LPSTR)"TestingMyDSN");
You will have to change the parameters to meet your DB settings and type (ie not SQLServer, file DSN ect).
I assume you know where to check the DSN settings under Administrative Tools.
I assume you know that some corp machines will have security to block these changes to the registry.
EDIT:
Each section of the string must be null terminated and so the end of the string will be double null terminated.
I have a loop that adds the params to a string as the user will change/supply the DSN settings in the app during runtime.
Code:
char szAttrib[1028]={'\0'};
char *pAttrib=NULL,*pParam=NULL;
_snprintf(szDSN,127,"DSN=%s",szSuppliedDSN);
_snprintf(szServer,127,"Server=%s",szSuppliedServer);
_snprintf(szDatabase,127,"Database=%s",szSuppliedDatabase);
_snprintf(szConnection,127,"Trusted_Connection=NO");
_snprintf(szDescription,127,"This is the DSN For %s",szAppName);
pAttrib=szAttrib;
for(i=0;i<5;i++)
{
switch(i)
{
case 0:
pParam=szDSN;
iLen=lstrlen(szDSN);
break;
case 1:
pParam=szServer;
iLen=lstrlen(szServer);
break;
case 2:
pParam=szDatabase;
iLen=lstrlen(szDatabase);
break;
case 3:
pParam=szConnection;
iLen=lstrlen(szConnection);
break;
case 4:
pParam=szDescription;
iLen=lstrlen(szDescription);
break;
}
//null terminate
pParam[iLen]='\0';
//add to the attrib string
strcpy(pAttrib,pParam);
pAttrib+=iLen+1;
}