⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.cpp

📁 wince下对sqlserver数据库的操作,增删改操作,单进程单线程
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}
	hr = pIDBCrtCmd->CreateCommand(NULL, IID_ICommandText, (IUnknown**)
		&pICmdText);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

	// Request the ability to use the IRowsetPosition interface.
	rowsetpropset[0].cProperties     = 1;
	rowsetpropset[0].guidPropertySet = DBPROPSET_ROWSET;
	rowsetpropset[0].rgProperties    = rowsetprop;

	rowsetprop[0].dwPropertyID   = DBPROP_CANFETCHBACKWARDS;
	rowsetprop[0].dwOptions      = DBPROPOPTIONS_REQUIRED;
	rowsetprop[0].vValue.vt      = VT_BOOL;
	rowsetprop[0].vValue.boolVal = VARIANT_TRUE;

	// Set the query text for the command.
	hr = pICmdText->SetCommandText(DBGUID_SQL, pSql); 
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

	hr = pICmdText->QueryInterface(IID_ICommandProperties, (void**) &pICmdProps);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}
	hr = pICmdProps->SetProperties(1, rowsetpropset);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

	// Creates an IRowsetPosition object for the returned rowset.
	hr = pICmdText->Execute(NULL, IID_IRowsetPosition, NULL, NULL,
		(IUnknown**)&pIRowsetPos);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	} // Get a count of the number or rows returned in the rowset.

	hr = pIRowsetPos->GetRecordCount(DB_NULL_HCHAPTER, &cbRecordCount);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	} 

	// Do something here with the aquired row count information.

Exit: 
	if(pIRowsetPos)
		pIRowsetPos->Release();
	if(pICmdProps)
		pICmdProps->Release();
	if(pIDBCrtCmd)
		pIDBCrtCmd->Release();
	if(pICmdText)
		pICmdText->Release();
	if(pIRowset)
		pIRowset->Release();
	if(FAILED(hr))
	{
		return ERR_BUS_SQLEXE;
	}else
		return cbRecordCount;
}
HRESULT CDataBase::PrintErrorInfo(REFIID riid, IUnknown *pComponent)
{
	IErrorInfo * pErrorInfo = NULL;
	IErrorInfo * pErrorInfoRec = NULL;
	IErrorRecords * pErrorRecords = NULL;
	ISupportErrorInfo * pSupportErrorInfo = NULL;
	HRESULT hr = E_FAIL;
	ULONG i, ulNumErrorRecs;
	ERRORINFO ErrorInfo;

	memset (&ErrorInfo, '\0', sizeof(ERRORINFO));

	hr = pComponent->QueryInterface(IID_ISupportErrorInfo, (LPVOID FAR*)
	&pSupportErrorInfo);
	if (SUCCEEDED(hr))
	{
		hr = pSupportErrorInfo->InterfaceSupportsErrorInfo(riid);
		if(hr == S_OK)
		{
			hr = GetErrorInfo(0, &pErrorInfo);
			if (!pErrorInfo)
			{
			hr = pSupportErrorInfo->Release();
			return (hr);
			}

			hr = pErrorInfo->QueryInterface(IID_IErrorRecords, (LPVOID FAR*)
			&pErrorRecords);
			hr = pErrorRecords->GetRecordCount(&ulNumErrorRecs);

			//If no error records but we are expecting one, then fail here
			if (0 == ulNumErrorRecs)
			{
			hr = pSupportErrorInfo->Release();
			hr = pErrorRecords->Release();
			return (hr);
			}
		}else 
			return (hr);
	}else 
		return (hr);

	for (i = 0; i<ulNumErrorRecs;i++)
	{
	//	GetBasicErrorInfo(i, &ErrorInfo);

		hr = pErrorRecords->GetErrorInfo(i, NULL, &pErrorInfoRec);
		BSTR bstrDescriptionOfError = NULL;
		BSTR bstrSourceOfError = NULL;

		hr = pErrorInfoRec->GetDescription(&bstrDescriptionOfError);
		hr = pErrorInfoRec->GetSource(&bstrSourceOfError);
		MessageBox(NULL,bstrSourceOfError, L"Casper", MB_OK);
	}
	return (hr);
}
/*
功能:根据指定的业务表及查询条件取记录
参数:
pBusTableStruct pBusStr--查询的业务表的结构
WCHAR *wcWhere --要查询的条件
RetResultStruct **ppResult--要返回的查询记录指针的指针
返回:int 
	< 0 --错误代码
	
	  >=0--查询结果记录个数
编写人:吕黄梁
时间:2004-12-23
*/
int CDataBase::GetCombRows(pBusTableStruct pBusStr,WCHAR *wcSql,RetResultStruct **ppResult)
{
	HRESULT     hr            = NOERROR;  
	DBBINDING			*prgBinding			= NULL;				// Binding used to create accessor
	HROW				rghRows[1];								// Array of row handles obtained from the rowset object
	HROW				*prghRows			= rghRows;			// Row handle(s) pointer
//	DBID				TableID;								// Used to open/create table
//	DBPROPSET   dbpropset[1]; 
//	DBPROP      dbprop[1]; 
	DBPROPSET   rowsetpropset[1]; 
	DBPROP      rowsetprop[2]; 
//	ULONG       cbRecordCount;
	DBCOLUMNINFO		*pDBColumnInfo		= NULL;				// Record column metadata
	BYTE				*pData				= NULL;				// record data
	WCHAR				*pStringsBuffer		= NULL;
	DWORD				dwBindingSize		= 0;
	DWORD				dwIndex				= 0;
	DWORD				dwOffset			= 0;
//	DWORD				dwOrdinal			= 0;
	ULONG				ulNumCols;
	WCHAR				wcTemp[CC_MAX_FIELDS_LEN];
	int nCount=0,i=0,index=0;
	int nSizeStr=0;
	char *pRowStr;
	RetResultStruct *pCurResult,*pNextResult;
	// Provider interfaces
	IRowsetPosition *    pIRowsetPos       = NULL;
	ICommandProperties * pICmdProps        = NULL;
	IDBCreateCommand *   pIDBCrtCmd        = NULL;
	ICommandText *       pICmdText         = NULL;
	IRowset *            pIRowset          = NULL;
	IAccessor			*pIAccessor			= NULL;				// Provider Interface Pointer
	ILockBytes			*pILockBytes		= NULL;				// Provider Interface Pointer
	IColumnsInfo		*pIColumnsInfo		= NULL;				// Provider Interface Pointer
	HACCESSOR			hAccessor			= DB_NULL_HACCESSOR;// Accessor handle

	// Initialize the environment.
	if (NULL == m_pIDBCreateSession)
	{
		ASSERT(0);
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		return ERR_PROG_NOTOPEN;//编程错误
	}
	hr = m_pIDBCreateSession->CreateSession(NULL, IID_IDBCreateCommand,
		(IUnknown**) &pIDBCrtCmd);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}
	hr = pIDBCrtCmd->CreateCommand(NULL, IID_ICommandText, (IUnknown**)
		&pICmdText);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

	// Request the ability to use the IRowsetPosition interface.
	rowsetpropset[0].cProperties     = 1;
	rowsetpropset[0].guidPropertySet = DBPROPSET_ROWSET;
	rowsetpropset[0].rgProperties    = rowsetprop;

	rowsetprop[0].dwPropertyID   = DBPROP_CANFETCHBACKWARDS;
	rowsetprop[0].dwOptions      = DBPROPOPTIONS_REQUIRED;
	rowsetprop[0].vValue.vt      = VT_BOOL;
	rowsetprop[0].vValue.boolVal = VARIANT_TRUE;

	// Set the query text for the command.
//	WCHAR SqlBuf[1024];
//	swprintf(SqlBuf,L"select * from %s %s;",pBusStr->Name,wcWhere);
	hr = pICmdText->SetCommandText(DBGUID_SQL, wcSql); 
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}
/*
	hr = pICmdText->QueryInterface(IID_ICommandProperties, (void**) &pICmdProps);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		goto Exit;
	}
	hr = pICmdProps->SetProperties(1, rowsetpropset);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		goto Exit;
	}
*/
	// Creates an IRowsetPosition object for the returned rowset.

	hr = pICmdText->Execute(NULL, IID_IRowset, NULL, NULL,
		(IUnknown**)&pIRowset);
	if (FAILED(hr))
	{
		//Send an error-specific message and do error handling.
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	} // Get a count of the number or rows returned in the rowset.

    // Get IColumnsInfo interface
	//
    hr = pIRowset->QueryInterface(IID_IColumnsInfo, (void **)&pIColumnsInfo);
	if(FAILED(hr))
	{
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

	// Get the column metadata 
	//
    hr = pIColumnsInfo->GetColumnInfo(&ulNumCols, &pDBColumnInfo, &pStringsBuffer);
	if(FAILED(hr) || 0 == ulNumCols)
	{
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

    // Create a DBBINDING array.
	//

	dwBindingSize = pBusStr->Num;//sizeof(pwszEmployees)/sizeof(pwszEmployees[0]);
	prgBinding = (DBBINDING*)CoTaskMemAlloc(sizeof(DBBINDING)*dwBindingSize);
	if (NULL == prgBinding)
	{
		hr = E_OUTOFMEMORY;
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

	// Set initial offset for binding position
	//
	dwOffset = 0;

	// Prepare structures to create the accessor
	//
    for (dwIndex = 0; dwIndex < dwBindingSize; ++dwIndex)
    {
	/*	if (!GetColumnOrdinal(pDBColumnInfo, ulNumCols, pwszEmployees[dwIndex], &dwOrdinal))
		{
			hr = E_FAIL;
			goto Exit;
		}
	*/
		// Prepare structures to create the accessor
		//
		prgBinding[dwIndex].iOrdinal	= pDBColumnInfo[dwIndex].iOrdinal;
		prgBinding[dwIndex].dwPart		= DBPART_VALUE | DBPART_STATUS | DBPART_LENGTH;
		prgBinding[dwIndex].obLength	= dwOffset;                                     
		prgBinding[dwIndex].obStatus	= prgBinding[dwIndex].obLength + sizeof(ULONG);  
		prgBinding[dwIndex].obValue		= prgBinding[dwIndex].obStatus + sizeof(DBSTATUS);
		prgBinding[dwIndex].pTypeInfo	= NULL;
		prgBinding[dwIndex].pBindExt	= NULL;
		prgBinding[dwIndex].dwMemOwner	= DBMEMOWNER_CLIENTOWNED;
		prgBinding[dwIndex].dwFlags		= 0;
		prgBinding[dwIndex].bPrecision	= pDBColumnInfo[dwIndex].bPrecision;
		prgBinding[dwIndex].bScale		= pDBColumnInfo[dwIndex].bScale;

		switch(pDBColumnInfo[dwIndex].wType)
		{
		case DBTYPE_BYTES:		// Column "Photo" binding (BLOB) 
			// Set up the DBOBJECT structure.
			//
			ASSERT(0);
			break;

		case DBTYPE_WSTR:
			prgBinding[dwIndex].pObject		= NULL;
			prgBinding[dwIndex].wType		= pDBColumnInfo[dwIndex].wType;
			prgBinding[dwIndex].cbMaxLen	= sizeof(WCHAR)*(pDBColumnInfo[dwIndex].ulColumnSize + 1);	// Extra buffer for null terminator 
			break;

		default:
			prgBinding[dwIndex].pObject		= NULL;
			prgBinding[dwIndex].wType		= pDBColumnInfo[dwIndex].wType;
			prgBinding[dwIndex].cbMaxLen	= pDBColumnInfo[dwIndex].ulColumnSize; 
			break;
		}

		// Calculate new offset
		// 
		dwOffset = prgBinding[dwIndex].obValue + prgBinding[dwIndex].cbMaxLen;

		// Properly align the offset
		//
		dwOffset = ROUND_UP(dwOffset, COLUMN_ALIGNVAL);
	}

	// Get IAccessor interface
	//
	hr = pIRowset->QueryInterface(IID_IAccessor, (void**)&pIAccessor);
	if(FAILED(hr))
	{
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

    // Create accessor.
	//
    hr = pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA, 
									dwBindingSize, 
									prgBinding,
									0,
									&hAccessor,
									NULL);
    if(FAILED(hr))
    {
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
        goto Exit;
    }

	// Allocate data buffer for seek and retrieve operation.
	//
	pData = (BYTE*)CoTaskMemAlloc(dwOffset);
	if (NULL == pData)
	{
		hr = E_OUTOFMEMORY;
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;
	}

    // Set data buffer to zero
    //
	// 取数据
    // Retrieve a row handle for the row resulting from the seek
    //计算结构长度
	pBusFieldStruct pField;
	pField=pBusStr->pBusFields;
	for(i=0;i<pBusStr->Num;i++,pField++)
	{
		nSizeStr+=pField->Length;
	}
	ULONG cRowsObtained;
    hr = pIRowset->GetNextRows(DB_NULL_HCHAPTER, 0, 1, &cRowsObtained, &prghRows);
	if(FAILED(hr))
	{
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
		goto Exit;	
	}
	while(DB_S_ENDOFROWSET != hr)
	{
		memset(pData, 0, dwOffset);
		hr = pIRowset->GetData(prghRows[0], hAccessor, pData);
		if (FAILED(hr))
		{
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
			goto Exit;
		}
		pNextResult =(RetResultStruct*)malloc(sizeof(RetResultStruct));
		memset(pNextResult,0,sizeof(RetResultStruct));
		pNextResult->nSize=nSizeStr;
		pNextResult->pBuf = malloc(nSizeStr);
		memset(pNextResult->pBuf,0,nSizeStr);
		if(nCount==0)
		{
			*ppResult = pNextResult;
			pCurResult=pNextResult;
		}else
		{
			pCurResult->pNext=pNextResult;
			pCurResult=pNextResult;
		}
		pField=pBusStr->pBusFields;
		pRowStr=(char*)pCurResult->pBuf;
#ifdef DEBUG
//		room_info_struct *pRoom=(room_info_struct*)pCurResult->pBuf;
#endif
		for(index=0;index<pBusStr->Num;index++,pRowStr+=pField->Length,pField++)
		{//取每个字段的值
			switch(pDBColumnInfo[index].wType)
			{
			case DBTYPE_BYTES:		// Column "Photo" binding (BLOB) 
				ASSERT(0);

			case DBTYPE_WSTR:
				memcpy(wcTemp,pData+prgBinding[index].obValue,prgBinding[index].cbMaxLen-2);
				memset(wcTemp+prgBinding[index].cbMaxLen-2,0,2);
				WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wcTemp,-1,pRowStr,pField->Length,NULL,NULL);
				break;
			default:
				memcpy(pRowStr,pData+prgBinding[index].obValue,prgBinding[index].cbMaxLen);
				break;
			}
		}


		nCount ++;
		pIRowset->ReleaseRows(1,prghRows,NULL,NULL,NULL);
		hr = pIRowset->GetNextRows(DB_NULL_HCHAPTER, 0/*nCount*/, 1, &cRowsObtained, &prghRows);
		if (FAILED(hr))
		{
		writebuginfo("file:%s,line:%d\r\n",__FILE__,__LINE__);
			goto Exit;
		}


	}
Exit: 

	VariantClear(&rowsetprop[0].vValue);

    // Free allocated DBBinding memory
    //
    if (prgBinding)
    {
        CoTaskMemFree(prgBinding);
        prgBinding = NULL;
    }

    // Free allocated column info memory
    //
    if (pDBColumnInfo)
    {
        CoTaskMemFree(pDBColumnInfo);
        pDBColumnInfo = NULL;
    }
    if (pStringsBuffer)
    {
        CoTaskMemFree(pStringsBuffer);
        pStringsBuffer = NULL;
    }
	

    // Free data record buffer
    //
	if (pData)
	{
        CoTaskMemFree(pData);
		pData = NULL;
	}

	if(pIAccessor)
	{
		pIAccessor->ReleaseAccessor(hAccessor, NULL); 
		pIAccessor->Release();
	}

	if (pIColumnsInfo)
	{
		pIColumnsInfo->Release();
	}


//	pIRowset->ReleaseRows(1,rghRows,NULL,NULL,NULL);
	if (prghRows)
	{
     //   CoTaskMemFree(prghRows);
      //  prghRows = NULL;
	}

	if(pIRowset)
	{
		pIRowset->Release();
	}

	if(pICmdText)
		pICmdText->Release();
	if(pIDBCrtCmd)
		pIDBCrtCmd->Release();


	if(FAILED(hr))
	{
		return ERR_BUS_SQLEXE;
	}else
		return nCount;
}

void CDataBase::ReplaceYinhao(WCHAR *p)
{
	WCHAR *ptemp=wcschr(p,L'\'');
	while(ptemp!= NULL)
	{
		for(int i=wcslen(ptemp);i>0;i--)
		{
			*(ptemp+i)=*(ptemp+i-1);
		}
		ptemp=wcschr(ptemp+2,L'\'');
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -