connecting SQL Database to my C++ application.

  • #include

    #include

    #include

    #include

    int main(int argc, char *argv[])

    {

    HENV hEnv = NULL; // Env Handle from SQLAllocEnv()

    HDBC hDBC = NULL; // Connection handle

    HSTMT hStmt = NULL;// Statement handle

    UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "CustomerDetails";// Data Source Namebuffer

    UCHAR szUID[13] = "username";// User ID buffer

    UCHAR szPasswd[7] = "passwd";// Password buffer

    UCHAR szModel[128];// Model buffer

    SDWORD cbModel;// Model buffer bytes recieved

    UCHAR szSqlStr[128]= "Select Cust_ID From CustomerDetails Where Cust_name='anand'";

    RETCODE retcode;

    // Allocate memory for ODBC Environment handle

    SQLAllocEnv (&hEnv);

    // Allocate memory for the connection handle

    SQLAllocConnect (hEnv, &hDBC);

    // Connect to the data source "szDSN" using userid and password.

    retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd,SQL_NTS);

    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)

    {

    // Allocate memory for the statement handle

    retcode = SQLAllocStmt (hDBC, &hStmt);

    // Prepare the SQL statement by assigning it to the statement handle

    retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));

    // Execute the SQL statement handle

    retcode = SQLExecute (hStmt);

    // Project only column 1 which is the models

    SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel),&cbModel);

    // Get row of data from the result set defined above in the statement

    retcode = SQLFetch (hStmt);

    // Free the allocated statement handle

    SQLFreeStmt (hStmt, SQL_DROP);

    // Disconnect from datasource

    SQLDisconnect (hDBC);

    }

    std::cout << retcode << "";

    // Free the allocated connection handle

    SQLFreeConnect (hDBC);

    // Free the allocated ODBC environment handle

    SQLFreeEnv (hEnv);

    system("PAUSE");

    return EXIT_SUCCESS;

    }

    This is the code when i compile the program i am getting linker errors

    what is meant by those errors? Can anyone help me out i removing those errors.

    11:34 C:\Documents and Settings\anand.k\Desktop\1.cpp [Warning] unknown escape sequence '\S'

    C:\DOCUME~1\anand.k\LOCALS~1\Temp\cci4aaaa.o(.text+0xb0) In function `main':

    [Linker error] undefined reference to `SQLAllocEnv@4'

    [Linker error] undefined reference to `SQLAllocConnect@8'

    [Linker error] undefined reference to `SQLConnect@28'

    [Linker error] undefined reference to `SQLAllocStmt@8'

    [Linker error] undefined reference to `SQLPrepare@12'

    [Linker error] undefined reference to `SQLExecute@4'

    [Linker error] undefined reference to `SQLBindCol@24'

    [Linker error] undefined reference to `SQLFetch@4'

    [Linker error] undefined reference to `SQLFetch@4'

    [Linker error] undefined reference to `SQLFreeStmt@8'

    [Linker error] undefined reference to `SQLDisconnect@4'

    [Linker error] undefined reference to `SQLFreeConnect@4'

    [Linker error] undefined reference to `SQLFreeEnv@4'

    11:34 C:\DOCUME~1\anand.k\LOCALS~1\Temp\cci4aaaa.o(.text+0xb0) ld returned 1 exit status

    Please help me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • i think you are using

    windows.h

    sql.h

    sqlext.h

    sqltypes.h

    because i cant see your include files x) anyway, are you sure the libs have a link to your project? look like missing libs

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply