Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
I am very new in connecting sql server to c++ program.
My Problem is the query that I run only return value in first row
and I want all rows' data in special column.

What I have tried:

if (SQL_SUCCESS != SQLExecDirect(hstmt, (SQLWCHAR*)L"SELECT Location FROM dbo.Customers", SQL_NTS)) {
					   		cout << "Error querying SQL Server";
					   		cout << "\n";
					   	}
				   else
				   {
						SQLCHAR sqlVersion[SQL_RESULT_LEN];
						SQLINTEGER ptrSqlVersion;
						while (SQLFetch(hstmt) == SQL_SUCCESS) {
							SQLGetData(hstmt, 2, SQL_CHAR, sqlVersion, SQL_RESULT_LEN, &ptrSqlVersion);
							//display query result
							cout << "\nQuery Result:\n\n";
							cout << sqlVersion << endl;
						}
				   }
Posted
Updated 6-Aug-18 0:28am

1 solution

Your code is not printing the value of the first row but the same random value for each row.

Your query specifies a single column only. So you can only get data for column one but you are asking for column 2. You would have noticed this when checking the GetData() return value.

So try this:
SQLRETURN ret = SQLGetData(hstmt, 1, SQL_CHAR, sqlVersion, sizeof(sqlVersion), &ptrSqlVersion);
// Check ret here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900