VC++ Code to List System DSN in your PC
Had a peculiar problem. I wanted to display the list of DSNs registred in a machine in a Java JComboBox.
As far as I know, Java in no way can get a list of System DSN.So thought of using a JNI program to do this.
Was trying a vc++ program to list out the System DSN List and I ended up writing this small program.
Create a win32 Console application in VC++, include MFC support (for proper working of SQLnnn functions)
Replace the code to your main c++/c file.Click to Download the Files.
// dsntest2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "dsntest2.h" //nothing much in this .h file, created by VC++
//file name same as your cpp file
#include
#include
#define DSN_BUFFER_LEN 256
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
//code to find out the system dsn list
int handle;
SQLHANDLE sqlHandle;
SQLSMALLINT nFirstDSN=SQL_FETCH_FIRST_SYSTEM; //if you want user dsns, assign
//SQL_FETCH_FIRST_USER
SQLSMALLINT nNextDSN =SQL_FETCH_NEXT;
SQLCHAR sDSName[DSN_BUFFER_LEN]; //dsn name
SQLCHAR sDrvrDtl[DSN_BUFFER_LEN];//driver info
SQLSMALLINT nDsnLen,nDtlLen;
handle = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&sqlHandle);
handle = SQLSetEnvAttr(sqlHandle,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0);
if(handle != SQL_SUCCESS){
cerr<<"Setting of ODBC Version Failed!";
nRetCode=1;
}
handle = SQLDataSources(sqlHandle ,nFirstDSN,sDSName,DSN_BUFFER_LEN,&nDsnLen,sDrvrDtl,DSN_BUFFER_LEN,&nDtlLen);
cout.setf(ios::left);
cout << setw(6)<<"DSN: "<< setw(15) << sDSName
<< setw(5)<<"Driver: "<< sDrvrDtl << endl;
while(handle == SQL_SUCCESS){
handle = SQLDataSources(sqlHandle ,nNextDSN,sDSName,DSN_BUFFER_LEN,&nDsnLen,sDrvrDtl,DSN_BUFFER_LEN,&nDtlLen);
cout << setw(6)<<"DSN: " << setw(15) << sDSName
<< setw(5)<<"Driver: " << sDrvrDtl << endl;
}
handle = SQLFreeHandle(SQL_HANDLE_ENV,sqlHandle);
cout << "\nThese are the system DSNs found in this Petti!\n\n";
nRetCode = 0;
}
return nRetCode;
}
|