Mostra o Erro: Run-Time Check Failure #2 - Stack around the variable ‘cpf’ was corrupted.
//-----------------------------------------------------------------------
O que ele imprime:
SQL SUCESS WITH INFO
ID: 0
Senha: 999
Nome: Lucas Pereira
CPF: 0
Saldo: 0
//-----------------------------------------------------------------------
Code:
#include “stdlib.h”
#include <locale.h>
#include
#include <windows.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <sql.h>
#define servidor “DRIVER={SQL Server}; SERVER=LAPTOP-7OKQPHKC; Trusted_Connection=Yes;”
using namespace std;
void showSQLError(unsigned int handleType, const SQLHANDLE& handle)
{
SQLCHAR SQLState[1024];
SQLCHAR message[1024];
if (SQL_SUCCESS == SQLGetDiagRec(handleType, handle, 1, SQLState, NULL, message, 1024, NULL))
// Returns the current values of multiple fields of a diagnostic record that contains error, warning, and status information
cout << "SQL driver message: " << message << "\nSQL state: " << SQLState << “.” << endl;
}
int main()
{
setlocale(LC_ALL, “portuguese”);
SQLHANDLE SQLEnvHandle = NULL;
SQLHANDLE SQLConnectionHandle = NULL;
SQLHANDLE SQLStatementHandle = NULL;
SQLRETURN retCode = 0;
char SQLQuery[] = “SELECT [ID], [Senha], [Nome], [CPF], [Saldo] FROM [Banco_APS_II].[dbo].[info_clientes_Banco] WHERE(info_clientes_Banco.ID LIKE 666) AND(info_clientes_Banco.Senha LIKE 999)”;
do {
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &SQLEnvHandle))
// Allocates the environment
break;
if (SQL_SUCCESS != SQLSetEnvAttr(SQLEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
// Sets attributes that govern aspects of environments
break;
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, SQLEnvHandle, &SQLConnectionHandle))
// Allocates the connection
break;
//problema
//if (SQL_SUCCESS != SQLSetConnectAttr(SQLConnectionHandle, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0))
// Sets attributes that govern aspects of connections
// break;
switch (SQLDriverConnect(SQLConnectionHandle,
NULL,
(SQLCHAR*)servidor,
SQL_NTS,
NULL,
0,
NULL,
SQL_DRIVER_COMPLETE)) {
// Establishes connections to a driver and a data source
case SQL_SUCCESS:
cout << "SQL CONECTADO!\n" << endl;
break;
case SQL_SUCCESS_WITH_INFO:
cout << "SQL SUCESS WITH INFO\n" << endl;
//showSQLError(SQL_HANDLE_DBC, SQLConnectionHandle);
break;
case SQL_NO_DATA_FOUND:
showSQLError(SQL_HANDLE_DBC, SQLConnectionHandle);
retCode = -1;
break;
case SQL_INVALID_HANDLE:
showSQLError(SQL_HANDLE_DBC, SQLConnectionHandle);
retCode = -1;
break;
case SQL_ERROR:
showSQLError(SQL_HANDLE_DBC, SQLConnectionHandle);
retCode = -1;
break;
default:
break;
}
if (retCode == -1)
break;
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, SQLConnectionHandle, &SQLStatementHandle))
// Allocates the statement
break;
if (SQL_SUCCESS != SQLExecDirect(SQLStatementHandle, (SQLCHAR*)SQLQuery, SQL_NTS)) {
// Executes a preparable statement
showSQLError(SQL_HANDLE_STMT, SQLStatementHandle);
break;
}
else {
float id = 0;
int senha = 0;
char nome[50] = "";
float cpf = 0;
float saldo = 0;
while (SQLFetch(SQLStatementHandle) == SQL_SUCCESS) {
// Fetches the next rowset of data from the result
SQLGetData(SQLStatementHandle, 1, SQL_C_DEFAULT, &id, sizeof(id), NULL);
SQLGetData(SQLStatementHandle, 2, SQL_C_DEFAULT, &senha, sizeof(senha), NULL);
SQLGetData(SQLStatementHandle, 3, SQL_C_DEFAULT, &nome, sizeof(nome), NULL);
SQLGetData(SQLStatementHandle, 4, SQL_C_DEFAULT, &cpf, sizeof(cpf), NULL);
SQLGetData(SQLStatementHandle, 5, SQL_C_DEFAULT, &saldo, sizeof(saldo), NULL);
// Retrieves data for a single column in the result set
printf("ID: %0.f\nSenha: %d\nNome: %s\nCPF: %0.f\nSaldo: %0.f\n", id, senha,nome,cpf,saldo);
}
}
} while (FALSE);
SQLFreeHandle(SQL_HANDLE_STMT, SQLStatementHandle);
SQLDisconnect(SQLConnectionHandle);
SQLFreeHandle(SQL_HANDLE_DBC, SQLConnectionHandle);
SQLFreeHandle(SQL_HANDLE_ENV, SQLEnvHandle);
// Frees the resources and disconnects
}