I am trying to connect SQL Server in C++ using below code but i am getting the same error "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
SQLHANDLE sqlenvhandle;
SQLHANDLE sqlconnectionhandle;
SQLHANDLE sqlstatementhandle;
SQLRETURN retcode;
do
{
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
break;
if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
break;
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
break;
SQLWCHAR retconstring[1024];
switch(SQLDriverConnect (sqlconnectionhandle, NULL,
(SQLWCHAR*)"DRIVER={SQL Server};SERVER=(IPADDRESS\\SQLEXPRESS);DATABASE=test;UID=sa;PWD=abcd$1234;",
SQL_NTS, retconstring, 1024, NULL,SQL_DRIVER_NOPROMPT))
{
case SQL_SUCCESS_WITH_INFO:
//show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
retcode = 0;
break;
case SQL_INVALID_HANDLE:
case SQL_ERROR:
//show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
SQLWCHAR sqlstate[1024];
SQLWCHAR message[1024];
if(SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_DBC, sqlconnectionhandle, 1, sqlstate, NULL, message, 1024, NULL))
{
retcode = -1;
break;
}
default:
break;
}
if(retcode == -1)
break;
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle))
break;
if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (SQLWCHAR*)"select * from testtable", SQL_NTS))
{
//show_error(SQL_HANDLE_STMT, sqlstatementhandle);
break;
}
else
{
char name[64];
char address[64];
int id;
while(SQLFetch(sqlstatementhandle)==SQL_SUCCESS)
{
SQLGetData(sqlstatementhandle, 1, SQL_C_ULONG, &id, 0, NULL);
SQLGetData(sqlstatementhandle, 2, SQL_C_CHAR, name, 64, NULL);
SQLGetData(sqlstatementhandle, 3, SQL_C_CHAR, address, 64, NULL);
}
}
}
while(FALSE);
SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );
SQLDisconnect(sqlconnectionhandle);
SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);
Current configuration is below:
- SQL Service is running.
- TCP/IP is also Enabled.
- System DNS Name is Set to "SQL Server" in ODBC.
- ODBC Source administrator target is "%windir%\System32\odbcad32.exe".
- Tried to replace connection string with IP Address and added port 1433 also.
but getting same error for all these settings.
Please let me know if i am missing anything or do i need to change my connection string or if there is any other way to connect SQL Server in C++.