Tyro wrote:
> Hi can you please tell me what is the error in this code?
>
>
> #include<windows.h>
> #include<iostream>
> #include<string>
> #include<sql.h>
> #include<sqlext.h>
> using namespace std;
> int main()
> {
>
> HENV hEnv;
> HDBC hDbc;
> RETCODE rc;
> int iOut;
> char strOut[256];
> char szDSN[256] = "driver={SQL
> Server};pwd={};Server={local};Database={C:\\Program Files\\Microsoft SQL
> Server\\MSSQL\\Data\\person_Data.MDF}";
>
> char* szSql = "select * from table1";
> rc = SQLAllocEnv(&hEnv);
> rc = SQLAllocConnect(hEnv, &hDbc);
>
> rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
> SQL_NTS, (unsigned char*)strOut,
> 255, (SQLSMALLINT*)&iOut, SQL_DRIVER_NOPROMPT);
> {
> int ival;
> char chval[128];
> int ret1;
> int ret2;
> HSTMT hStmt;
> rc = SQLAllocStmt(hDbc,&hStmt);
> rc = SQLPrepare(hStmt,(unsigned char*)szSql, SQL_NTS);//1
> //rc = SQLBindCol(hStmt, tab_column, tr_type, tr_value, tr_len,
> len_or_ind);
> rc = SQLBindCol(hStmt, 1, SQL_C_ULONG, &ival, 4, (SQLINTEGER*)& ret1);
> rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval, 128, (SQLINTEGER*)&ret2);
> rc = SQLExecute(hStmt); //2
>
>
> cout<< ">table:"<< endl;
> while(1) //3
> {
> rc = SQLFetch(hStmt);
> if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)break;
> cout<< "{"<< ival<<"}{"<< chval<< "}"<< endl;
> }
> rc=SQLFreeStmt(hStmt, SQL_DROP);
> }
> rc = SQLDisconnect(hDbc);
> rc = SQLFreeEnv(hEnv);
> return 0;
> }
>
>
> thanx
You aren't checking for errors. Also, I'm pretty sure you have to bind
columns to variables AFTER executing the query. Also, you should write
your while loop more like this:
rc = SQLFetch(hStmt);
while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
{
cout<< "{"<< ival<<"}{"<< chval<< "}"<< endl;
rc = SQLFetch(hStmt);
}
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* VerifyMyPC 2.2
Change tracking and management tool.
Reduce tech. support times from 2 hours to 5 minutes.
Free for personal use, $10 otherwise.
http://www.CubicleSoft.com/VerifyMyPC/