论坛徽章: 0
20 可用积分
先看下
int db2cliVar::SQLBindCol(SQLHSTMT hstmt, SQLUSMALLINT colNo)
{
// Variables storing the column information
char szColName[128]; // Column name
SQLSMALLINT nLen = 0; // Column name length
SQLSMALLINT nType = 0; // SQL data type
SQLUINTEGER colSize = 0; // Column size
SQLSMALLINT nScale, nNull; // Decimal digits & nullable
// Retrieve column information from CLI driver
SQLRETURN cliRC = SQLDescribeCol(hstmt, colNo,
(SQLCHAR*)szColName,
128,
&nLen,
&nType,
&colSize,
&nScale,
&nNull);
if (cliRC != SQL_SUCCESS && cliRC != SQL_SUCCESS_WITH_INFO)
return cliRC;
SQLSMALLINT bindType = 0; // Local data type to bind
SQLPOINTER bindPtr = NULL; // Bind variable address
SQLINTEGER bindLen = 0; // Length of bind variable
SQLINTEGER *bindNulInd = &m_nNullInd; // Null indicator variable
// Determin bind method according to SQL data type
switch (nType)
{
case SQL_REAL:
case SQL_FLOAT:
// Float values
m_nType = VT_FLOAT;
bindType = SQL_C_FLOAT;
bindPtr = (SQLPOINTER)&m_float;
bindLen = sizeof(float);
break;
case SQL_DOUBLE:
// Double precision
m_nType = VT_DOUBLE;
bindType = SQL_C_DOUBLE;
bindPtr = (SQLPOINTER)&m_double;
bindLen = sizeof(double);
break;
case SQL_INTEGER:
// 32-bit integer
m_nType = VT_INT;
bindType = SQL_C_LONG;
bindPtr = (SQLPOINTER)&m_long;
bindLen = sizeof(long);
break;
case SQL_SMALLINT:
// 16-bit integer
m_nType = VT_SHORT;
bindType = SQL_C_SHORT;
bindPtr = (SQLPOINTER)&m_short;
bindLen = sizeof(short);
break;
case SQL_TINYINT:
case SQL_BIT:
// 8-bit integer (char)
m_nType = VT_CHAR;
bindType = SQL_C_BIT;
bindPtr = (SQLPOINTER)&m_char;
bindLen = sizeof(m_char);
break;
case SQL_BIGINT:
// 64-bit integer
m_nType = VT_BIGINT;
bindType = SQL_C_SBIGINT;
bindPtr = (SQLPOINTER)&m_int64;
bindLen = sizeof(m_int64);
break;
case SQL_BINARY:
// Binary data
m_nType = VT_BINARY;
bindType = SQL_C_CHAR;
m_vchar.resize(colSize + 1);
bindPtr = (SQLPOINTER)&m_vchar[0];
bindLen = colSize;
break;
case SQL_TYPE_DATE:
// DATE_STRUCT
m_nType = VT_DATE;
bindType = SQL_C_DATE;
bindPtr = (SQLPOINTER)&m_date;
bindLen = sizeof(m_date);
break;
case SQL_TYPE_TIME:
// TIME_STRUCT
m_nType = VT_TIME;
bindType = SQL_C_TIME;
bindPtr = (SQLPOINTER)&m_time;
bindLen = sizeof(m_time);
break;
case SQL_TYPE_TIMESTAMP:
// TIMESTAMP_STRUCT
m_nType = VT_TIMESTAMP;
bindType = SQL_C_TIMESTAMP;
bindPtr = (SQLPOINTER)&m_timestamp;
bindLen = sizeof(m_timestamp);
break;
case SQL_CHAR:
case SQL_DECIMAL:
case SQL_LONGVARBINARY:
case SQL_LONGVARCHAR:
case SQL_NUMERIC:
case SQL_VARBINARY:
case SQL_VARCHAR:
case SQL_WCHAR:
default:
// Character string
m_nType = VT_CSTR;
bindType = SQL_C_CHAR;
m_vchar.resize(colSize + 4);
bindPtr = (SQLPOINTER)&m_vchar[0];
bindLen = colSize + 4;
}
return ::SQLBindCol(hstmt, colNo, bindType, bindPtr, bindLen, bindNulInd);
}
这个绑定函数是db2cliVar类里面的,m_long,m_vchar全部为类里面的全局变量,
char m_char;
unsigned char m_uchar;
short m_short;
unsigned short m_ushort;
int m_int;
unsigned int m_uint;
SQLINTEGER m_long;
unsigned long m_ulong;
float m_float;
double m_double;
SQLBIGINT m_int64;
unsigned __int64 m_uint64;
TIME_STRUCT m_time;
TIMESTAMP_STRUCT m_timestamp;
DATE_STRUCT m_date;
在这个db2cli类里面调用
int db2cli::GetReslut()
{
/*
* Bind the output parameter.
*/
cBindData.resize(ciFieldNum);
for(int i = 1; i<= ciFieldNum; i++)
{
db2cliVar &lcdVar = cBindData[i-1];//此处
cRet = lcdVar.SQLBindCol(chStmt, i);
if( cRet == SQL_ERROR )
{
GetErrMsg();
return -1;
}
}
return 0;
}
其中声明vector<db2cliVar>cBindData;
我是想每个列绑定cBindData数组里面一个类的一个变量,但是总是出现异常,运行时没有,单步调式时才有,下面是信息
我来回答