none
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified IM002

    Question

  • I am using VS2010 C++ to complie and run  a console app to connected with local installed SQLEXPRESS database. while I use control panel ODBC data souce check the connection , I have no problem at all and I can also use SQL server management studio to connect database, but while I run the program, I always get [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified IM002 error.

    I have tried all suggestion from previous  posting, check SQL native client installation, reinstall SQL EXPRESS, non of them help me, can any help me out?

    bellow is my related program.

    #include

    <iostream>

    #include

    <windows.h>

    #include

    <sqltypes.h>

    #include

    <sql.h>

    #include

    <sqlext.h>

    #include

    <string.h>

    using

    namespace std;

    void

    show_error(unsigned int handletype, const SQLHANDLE& handle){

    //SQLCHAR sqlstate[1024];

    TCHAR sqlstate[1024];

    //SQLCHAR message[1024];

    TCHAR message[1024];

    if(SQL_SUCCESS == SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL))

    cout<<

    "Message: "<<message<<"\nSQLSTATE: "<<sqlstate<<endl;

    }

    int

    main(){

    SQLHANDLE sqlenvhandle;

    SQLHANDLE sqlconnectionhandle;

    SQLHANDLE sqlstatementhandle;

    SQLRETURN retcode;

    if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))

    goto FINISHED;

    if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))

    goto FINISHED;

    if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))

    goto FINISHED;

    //SQLCHAR retconstring[1024];

    TCHAR retconstring[1024];

     

    switch(SQLDriverConnect (sqlconnectionhandle,

    NULL,

    (TCHAR*)

    "DRIVER={SQL Server};SERVER=localhost,1433;DATABASE=NewDB;UID=sa;PWD=password;",

    SQL_NTS,

    retconstring,

    1024,

    NULL,

    SQL_DRIVER_NOPROMPT)){

    case SQL_SUCCESS_WITH_INFO:

    show_error(SQL_HANDLE_DBC, sqlconnectionhandle);

    break;

    case SQL_INVALID_HANDLE:

    case SQL_ERROR:

    show_error(SQL_HANDLE_DBC, sqlconnectionhandle);

    goto FINISHED;

    default:

    break;

    }

    if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle))

    goto FINISHED;

    if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (TCHAR*)"select * from testtable", SQL_NTS)){

    show_error(SQL_HANDLE_STMT, sqlstatementhandle);

    goto FINISHED;

    }

    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);

    cout<<id<<

    " "<<name<<" "<<address<<endl;

    }

    }

    FINISHED:

    SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );

    SQLDisconnect(sqlconnectionhandle);

    SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);

    SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);

    }

    mardi 6 mars 2012 22:23

Réponses

  • Hi Ruizhao,

    Could you please post the full text error message here? It seems that it is a 5701 message, which indicates that SQL Server initially puts the user’s context into the target database specified on the connection string. It can be purely informational, and the application can ignore that. For more information, please refer to this KB article: INF: ODBC Messages on SQL Server Connections.

    In addition, if you want to use SQL Server Native Client as the provider, you may need to use ‘SQL Server Native Client’ (SQL Server 2005) or ‘SQL Server Native Client 10.0’ (SQL Server 2008 or R2) as the driver on your connection string. Please see: Using the SQL Server Native Client Header and Library Files.


    Stephanie Lv

    TechNet Community Support

    • Marqué comme réponse Stephanie Lv vendredi 9 mars 2012 00:58
    jeudi 8 mars 2012 03:01

Toutes les réponses

  • Please if somebody can help me out!
    mercredi 7 mars 2012 14:34
  • I have changed as bellow and get it work but still get 0100, [Microsoft]{ODBC SQL Server Driver][SQL Server]Changed database context to 'NewDB'

    (TCHAR*)TEXT(

    "DRIVER={SQL Server};SERVER=localhost,1433;DATABASE=NewDB;UID=sa;PWD=password;")

    if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (TCHAR*)TEXT("select * from testtable"), SQL_NTS)){

    mercredi 7 mars 2012 18:52
  • Hi Ruizhao,

    Could you please post the full text error message here? It seems that it is a 5701 message, which indicates that SQL Server initially puts the user’s context into the target database specified on the connection string. It can be purely informational, and the application can ignore that. For more information, please refer to this KB article: INF: ODBC Messages on SQL Server Connections.

    In addition, if you want to use SQL Server Native Client as the provider, you may need to use ‘SQL Server Native Client’ (SQL Server 2005) or ‘SQL Server Native Client 10.0’ (SQL Server 2008 or R2) as the driver on your connection string. Please see: Using the SQL Server Native Client Header and Library Files.


    Stephanie Lv

    TechNet Community Support

    • Marqué comme réponse Stephanie Lv vendredi 9 mars 2012 00:58
    jeudi 8 mars 2012 03:01
  • Hi Stephanie,

    The full text error is like this 0100, [Microsoft]{ODBC SQL Server Driver][SQL Server]Changed database context to 'NewDB'. I think you are right this is just pure information because I can still get further SQL database operation done such as the query.

    Thanks any way!

    jeudi 8 mars 2012 20:29