📄 db.cpp
字号:
SQLBindCol(pCon->hStmt, (UWORD)(nCount+1), SQL_C_CHAR, stData.Data[nCount], MAX_MIDDLECOL_LENGTH, &(stData.Length[nCount]));
/* // call SQLDescribeCol to get description of each column in the
// result set. Store the column name in the display buffer and
// make it the first entry in the results list box of the MDI
// child window.
for(nCount=1; nCount <= nCols; nCount++)
{
SQLDescribeCol(pCon->hStmt, nCount, szBuffer, MAX_DATA_LEN, &swColLength, &swColType, &udwColDef,
&swColScale, &swColNull);
}
*/
// call SQLFetch to fetch each row of the result set. Extract data
// value and store it in the display buffer, separated by tabs.
// continue until SQLFetch fails.
DWORD nCountRow = 0;
for(; (nReturn = SQLFetch(pCon->hStmt))==SQL_SUCCESS || nReturn==SQL_SUCCESS_WITH_INFO;)
{
if (nCountRow == MAX_MIDDLEROW_NUM) break;
memcpy(&(RealData[nCountRow]),&stData,sizeof(MIDDLEQUERYST));
nCountRow++;
if (nReturn != SQL_SUCCESS)
{
m_pWriteMessage(szBuffer);
DisplaySQLError(pCon);
}
}
// if there was any error returned by SQLFetch, display it
if (nReturn == SQL_ERROR)
{
m_pWriteMessage(szBuffer);
DisplaySQLError(pCon);
}
// Close the open result set.
// Unbind all bound columns.
SQLCloseCursor(pCon->hStmt);
SQLFreeStmt(pCon->hStmt, SQL_UNBIND);
SQLFreeStmt(pCon->hStmt, SQL_RESET_PARAMS);
// pCon->hStmt = NULL;
memcpy(pNumRow,&nCountRow,4);
return RealData;
}
////////////////////////////////////////////////////////////////////////////////////////////
// 磊蜡Large孽府 窃荐(SELECT巩 傈侩)
// 牢磊汲疙
// 目池记, 孽府巩, 馆券登绰 饭内靛狼 箭磊甫 淬阑 DWORD狼 器牢磐
// For Large Data(Ex: Board)
////////////////////////////////////////////////////////////////////////////////////////////
LPLARGEQUERY FAR PASCAL CDB::FreeLargeQuery(LPDBCON pCon,LPSTR szBuffer, LPDWORD pNumRow)
{
SWORD nCols = 0; // # of result columns
SWORD nCount; // index
SQLRETURN nReturn; // return code
if (pCon->hStmt == NULL)
{
nReturn = SQLAllocHandle(SQL_HANDLE_STMT, pCon->hDBC, &(pCon->hStmt));
if(nReturn != SQL_SUCCESS && nReturn != SQL_SUCCESS_WITH_INFO)
{
m_pWriteMessage(szBuffer);
DisplaySQLError(pCon);
}
}
// execute SQL and process errors if any
// call SQLExecDirect to execute the SQL statement
nReturn = SQLExecDirect(pCon->hStmt, (UCHAR *)szBuffer, SQL_NTS);
if (nReturn != SQL_SUCCESS)
{
m_pWriteMessage(szBuffer);
DisplaySQLError(pCon);
// if the return value was just information, carry on
if (nReturn != SQL_SUCCESS_WITH_INFO)
{
// Close the open result set.
SQLCloseCursor(pCon->hStmt);
return (LPLARGEQUERY) NULL;
}
}
// call SQLNumResultCols to calculate the number of columns in
// the result set, if more than the MAX_COL (the array boundary)
// limit the number to MAX_COL and display truncation warning.
// if it is 0, the statement probably was a non-SELECT simply return
nReturn = SQLNumResultCols(pCon->hStmt, &nCols);
if (nCols >= MAX_LARGECOL_NUM)
{
nCols = MAX_LARGECOL_NUM;
}
else if (nCols == 0)
{
// Close the open result set.
SQLCloseCursor(pCon->hStmt);
return (LPLARGEQUERY) NULL;
}
// 单捞鸥甫 淬扁 困茄 皋葛府甫 且寸茄促.
LARGEQUERYST stData;
memset(&stData,0,sizeof(LARGEQUERYST));
stData.nNum = nCols;
LPLARGEQUERY RealData = (LPLARGEQUERY) m_pResultMemory->Alloc();
if(!RealData)
{
m_pWriteMessage("FreeLargeQuery : Fail to alloc memory");
return (LPLARGEQUERY) NULL;
}
memset(RealData,0,sizeof(LARGEQUERYST));
// call SQLBindCol to bind column data array and column data length
// array to the result set
for(nCount=0; nCount<nCols; nCount++)
SQLBindCol(pCon->hStmt, (UWORD)(nCount+1), SQL_C_CHAR, stData.Data[nCount], MAX_LARGECOL_LENGTH, &(stData.Length[nCount]));
/* // call SQLDescribeCol to get description of each column in the
// result set. Store the column name in the display buffer and
// make it the first entry in the results list box of the MDI
// child window.
for(nCount=1; nCount <= nCols; nCount++)
{
SQLDescribeCol(pCon->hStmt, nCount, szBuffer, MAX_DATA_LEN, &swColLength, &swColType, &udwColDef,
&swColScale, &swColNull);
}
*/
// call SQLFetch to fetch each row of the result set. Extract data
// value and store it in the display buffer, separated by tabs.
// continue until SQLFetch fails.
DWORD nCountRow = 0;
for(; (nReturn = SQLFetch(pCon->hStmt))==SQL_SUCCESS || nReturn==SQL_SUCCESS_WITH_INFO;)
{
if (nCountRow == 1) break; // Large Query can Get Only One Row. Written By HaeSun
memcpy(&(RealData[nCountRow]),&stData,sizeof(LARGEQUERYST));
nCountRow++;
if (nReturn != SQL_SUCCESS)
{
m_pWriteMessage(szBuffer);
DisplaySQLError(pCon);
}
}
// if there was any error returned by SQLFetch, display it
if (nReturn == SQL_ERROR)
{
m_pWriteMessage(szBuffer);
DisplaySQLError(pCon);
}
// Close the open result set.
// Unbind all bound columns.
SQLCloseCursor(pCon->hStmt);
SQLFreeStmt(pCon->hStmt, SQL_UNBIND);
SQLFreeStmt(pCon->hStmt, SQL_RESET_PARAMS);
// pCon->hStmt = NULL;
memcpy(pNumRow,&nCountRow,4);
return RealData;
}
////////////////////////////////////////////////////////////////////////////////////////////
// 官牢爹孽府 窃荐(SELECT巩 傈侩)
// 牢磊汲疙
// 目池记, 孽府巩, LPBIND屈 器牢磐, 掘绊磊窍绰 饭内靛肮荐(弥措), 掘绊磊窍绰 饭内靛 胶飘钒媚狼 农扁, 鸥涝(胶飘傅? 箭磊?)
// "select City, FirstName from employees" 鞍篮 侥栏肺 孽府巩阑 父甸绢 持绢霖促.
// 捞 窃荐绰 困狼 橇府孽府客 崔府 流立 掘绊磊窍绰 单捞磐狼 皋葛府甫 棱篮 饶俊
// 窃荐甫 角青秦 芭扁俊 蔼阑 盲况持绰促.
// 捞巴捞 捞弗官 Row -Wise Binding 规侥捞促.
// 荤侩抗绰 VK_F5 曼炼
// 父老 漠烦喊肺 磊丰屈捞 促甫 版快俊绰 固府 LPBIND狼 QueryType俊促啊 弊 屈阑 持绢滴绊
// sqlType俊绰 NULL阑 持绢林搁 舅酒辑 等促.
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CDB::BindQuery(LPDBCON pCon,LPSTR szBuffer, LPBIND lpBind, int Rowset, int StSize, SQLSMALLINT sqlType)
{
SQLRETURN retCode; // return code
SQLUINTEGER NumRowsFetched;
SQLUSMALLINT RowStatusArray[MAX_ROW_NUM];
if (pCon->hStmt == NULL)
{
retCode = SQLAllocHandle(SQL_HANDLE_STMT, pCon->hDBC, &(pCon->hStmt));
if(retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO)
DisplaySQLError(pCon);
}
SQLSetStmtAttr(pCon->hStmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER) StSize, 0);
SQLSetStmtAttr(pCon->hStmt, SQL_ATTR_ROW_ARRAY_SIZE,(SQLPOINTER) Rowset, 0);
SQLSetStmtAttr(pCon->hStmt, SQL_ATTR_ROW_STATUS_PTR, RowStatusArray, 0);
SQLSetStmtAttr(pCon->hStmt, SQL_ATTR_ROWS_FETCHED_PTR, &NumRowsFetched, 0);
// 皋葛府 官牢爹
for (int k = 0;k < lpBind->nColNum;k++)
{
if (sqlType == NULL)
SQLBindCol(pCon->hStmt,k+1,lpBind->QueryType[k], ((LPROWWISEBIND) lpBind->pData[0])->Data[k],((LPROWWISEBIND) lpBind->pData[0])->Length[k],&(((LPROWWISEBIND) lpBind->pData[0])->LengthRead[k]));
else SQLBindCol(pCon->hStmt,k+1,sqlType, ((LPROWWISEBIND) lpBind->pData[0])->Data[k],((LPROWWISEBIND) lpBind->pData[0])->Length[k],&(((LPROWWISEBIND) lpBind->pData[0])->LengthRead[k]));
}
// 角青
retCode = SQLExecDirect(pCon->hStmt, (UCHAR *)szBuffer, SQL_NTS);
// 单捞磐 菩莫
while ((retCode = SQLFetchScroll(pCon->hStmt,SQL_FETCH_NEXT,0)) != SQL_NO_DATA)
{
// 鞠巴滴 且霸 诀促..-_-;
}
if (retCode == SQL_ERROR)
DisplaySQLError(pCon);
// Close the open result set.
// Unbind all bound columns.
retCode = SQLCloseCursor(pCon->hStmt);
SQLFreeHandle(SQL_HANDLE_STMT,pCon->hStmt);
// retCode = SQLFreeStmt(pCon->hStmt, SQL_RESET_PARAMS);
// retCode = SQLFreeStmt(pCon->hStmt, SQL_UNBIND);
pCon->hStmt = NULL;
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////
// 教臂 孽府 窃荐
// 牢磊汲疙
// 目池记, 抛捞喉 捞抚, IDENTITY 漠烦狼 蔼, 炼荤且 漠烦狼 捞抚, 单捞磐 鸥涝
// 捞 窃荐绰 角力利栏肺 啊厘 腹捞 静老 芭 鞍促.
// 抛捞喉付促 IDENTITY漠烦捞 粮犁窍绰单 捞巴篮 蜡老茄 蔼捞促.(抗甸 甸搁 蜡历酒捞叼)
// 蝶扼辑 捞 蔼阑 炼雀秦辑 窜 窍唱狼 饭内靛俊 措茄 沥焊父 罐酒坷绰 巴捞促.
// lpCol俊 NULL阑 持栏搁 秦寸 饭内靛狼 葛电 郴侩阑 府畔窍绊
// 漂沥 漠烦阑 瘤沥窍搁 弊 漠烦狼 郴侩父阑 逞败霖促.
// 促吝漠烦 孽府 啊瓷
// 林狼!!!!
// 郴何俊辑 皋葛府甫 棱酒辑 器牢磐甫 逞扁骨肺 罐酒辑 荤侩茄 饶 怖 皋葛府甫 钱绢拎具 茄促.!!!!!!!!!!!!!!
// 捞巴捞 捞弗官 Column -Wise Binding 规侥捞促.
////////////////////////////////////////////////////////////////////////////////////////////
LPQUERY FAR PASCAL CDB::SingleQuery(LPDBCON pCon, LPSTR szTable, DWORD dwValue, LPSTR lpCol, SQLSMALLINT sqltype)
{
SWORD nCols = 0; // # of result columns
SWORD nCount; // index
SQLRETURN retCode; // return code
if (pCon->hStmt == NULL)
{
retCode = SQLAllocHandle(SQL_HANDLE_STMT, pCon->hDBC, &(pCon->hStmt));
if(retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO)
DisplaySQLError(pCon);
}
// 巩厘 备己
char szBuffer[MAX_LENGTH_QUERY];
if (lpCol == NULL || *lpCol == 0)
sprintf(szBuffer,"select * from %s where IDENTITYCOL = '%d' " ,szTable,dwValue);
else sprintf(szBuffer,"select %s from %s where IDENTITYCOL = '%d' " ,lpCol, szTable, dwValue);
// execute SQL and process errors if any
// call SQLExecDirect to execute the SQL statement
retCode = SQLExecDirect(pCon->hStmt, (UCHAR *)szBuffer, SQL_NTS);
if (retCode != SQL_SUCCESS)
{
DisplaySQLError(pCon);
// if the return value was just information, carry on
if (retCode != SQL_SUCCESS_WITH_INFO)
{
// Close the open result set.
SQLCloseCursor(pCon->hStmt);
return (LPQUERY) NULL;
}
}
retCode = SQLNumResultCols(pCon->hStmt, &nCols);
if (nCols >= MAX_COL_NUM)
{
nCols = MAX_COL_NUM;
}
else if (nCols == 0)
{
// Close the open result set.
SQLCloseCursor(pCon->hStmt);
return (LPQUERY) NULL;
}
// 单捞鸥甫 淬扁 困茄 皋葛府甫 且寸茄促.
// LPQUERY RealData = new QUERYST;
LPQUERY RealData = (LPQUERY) m_pResultMemory->Alloc();
if(!RealData) m_pWriteMessage("SingleQuery : Fail to alloc memory");
memset(RealData,0,sizeof(QUERYST));
RealData->nNum = nCols;
for(nCount=0; nCount<nCols; nCount++)
SQLBindCol(pCon->hStmt, (UWORD)(nCount+1), sqltype, RealData->Data[nCount], MAX_COL_LENGTH, &(RealData->Length[nCount]));
for(; (retCode = SQLFetch(pCon->hStmt))==SQL_SUCCESS || retCode==SQL_SUCCESS_WITH_INFO;)
{
if (retCode != SQL_SUCCESS)
DisplaySQLError(pCon);
}
// if there was any error returned by SQLFetch, display it
if (retCode == SQL_ERROR)
DisplaySQLError(pCon);
// Close the open result set.
// Unbind all bound columns.
SQLCloseCursor(pCon->hStmt);
SQLFreeStmt(pCon->hStmt, SQL_UNBIND);
SQLFreeStmt(pCon->hStmt, SQL_RESET_PARAMS);
return RealData;
}
////////////////////////////////////////////////////////////////////////////////////////////
// 教臂 孽府DA 窃荐
// 傲 教臂孽府 窃荐客 促弗 痢篮 皋葛府啊 悼利栏肺 且寸等促绰 痢捞促...-_-
// 蝶扼辑 荤侩过捞 炼陛 汗棱茄 措脚 皋葛府甫 酒偿 荐 乐促.
// 牢磊汲疙
// 目池记, 抛捞喉 捞抚, IDENTITY 漠烦狼 蔼, 炼荤且 漠烦狼 捞抚, 单捞磐 鸥涝
// 促弗 汲疙篮 困狼 SingleQuery窃荐 汲疙 曼炼
// 促弗 痢篮 府畔蔼捞 LPQUERYDA 器牢磐牢单 捞巴篮 角力 单捞磐狼 器牢磐啊 甸绢乐绰 备炼眉捞促.
// 皋葛府 且寸档 蝶肺 柳青窍骨肺
// Free 且 锭俊档 馆靛矫 基敲俊 乐绰 巴贸烦 蝶肺 钱绢拎具 茄促.
// 荤侩过篮 SamlpeSingleQueryDA 曼炼
////////////////////////////////////////////////////////////////////////////////////////////
LPQUERYDA FAR PASCAL CDB::SingleQueryDA(LPDBCON pCon, LPSTR szTable, DWORD dwValue, LPSTR lpCol, SQLSMALLINT sqltype)
{
SWORD nCols = 0; // # of result columns
SWORD nCount; // index
SQLRETURN retCode; // return code
if (pCon->hStmt == NULL)
{
retCode = SQLAllocHandle(SQL_HANDLE_STMT, pCon->hDBC, &(pCon->hStmt));
if(retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO)
DisplaySQLError(pCon);
}
// 巩厘 备己
char szBuffer[MAX_LENGTH_QUERY];
if (lpCol == NULL || *lpCol == 0)
sprintf(szBuffer,"select * from %s where IDENTITYCOL = '%d' " ,szTable,dwValue);
else sprintf(szBuffer,"select %s from %s where IDENTITYCOL = '%d' " ,lpCol, szTable, dwValue);
retCode = SQLExecDirect(pCon->hStmt, (UCHAR *)szBuffer, SQL_NTS);
if (retCode != SQL_SUCCESS)
{
DisplaySQLError(pCon);
if (retCode != SQL_SUCCESS_WITH_INFO)
{
SQLCloseCursor(pCon->hStmt);
return (LPQUERYDA) NULL;
}
}
retCode = SQLNumResultCols(pCon->hStmt, &nCols);
if (nCols >= MAX_COL_NUM)
{
nCols = MAX_COL_NUM;
}
else if (nCols == 0)
{
SQLCloseCursor(pCon->hStmt);
return (LPQUERYDA) NULL;
}
LPQUERYDA RealData = new QUERYDA;
LPDBSDWORD pLength = new DBSDWORD[nCols];
DBSTRING* pData = new DBSTRING[nCols];
memset(RealData,0,sizeof(QUERYDA));
memset(pLength,0,sizeof(DBSDWORD)*nCols);
memset(pData,0,sizeof(DBSTRING)*nCols);
RealData->nNum = nCols;
RealData->pData = pData;
RealData->pLength = pLength;
for(nCount=0; nCount<nCols; nCount++)
SQLBindCol(pCon->hStmt, (UWORD)(nCount+1), sqltype, (&pData[nCount])->szData, MAX_COL_LENGTH, &( (&(pLength[nCount]))->pLeng));
for(; (retCode = SQLFetch(pCon->hStmt))==SQL_SUCCESS || retCode==SQL_SUCCESS_WITH_INFO;)
{
if (retCode != SQL_SUCCESS)
DisplaySQLError(pCon);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -