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

📄 oledb.cpp

📁 电脑编程技巧和源码。很不错的。
💻 CPP
字号:
// OleDb.cpp: implementation of the COleDb class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Monitor.h"
#include "OleDb.h"

#define NUMROWS_CHUNK 30

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

COleDb::COleDb()
{

}

COleDb::~COleDb()
{

}

HRESULT COleDb::OpenDB(CDataSource *DataSource,CSession *Session,LPCTSTR ProgID
		,LPCTSTR DataBase,LPCTSTR UserName,LPCTSTR Pass)
{
	HRESULT hr;
	hr = DataSource->Open(ProgID,DataBase,UserName,Pass);
//  hr = m_DataSource.Open(_T("MSDASQL"),"guest","","");
	if(FAILED(hr))
	{
		AfxMessageBox("连接数据库失败,退出程序");
		DataSource->Close();
		CoUninitialize();
		return hr;
	}
	hr=Session->Open(*DataSource);
	if(FAILED(hr))
    {
		AfxMessageBox("会话失败,退出程序");
		Session->Close();
		DataSource->Close();
		CoUninitialize();
		return hr;
	}
	return hr;
}

HRESULT COleDb::Excute(CSession *Session, IOpenRowset *pIOpenRowset,IDBCreateCommand *pICreateCommand,
					   ICommandText *pICommandText,IRowset** pIRowset,LPCOLESTR strCmd,LONG *cNumRows)
{
	HRESULT hr;
	pIOpenRowset=Session->m_spOpenRowset;
	hr=pIOpenRowset->QueryInterface(IID_IDBCreateCommand,
		(void **)&pICreateCommand);
    if(FAILED(hr))
    {
		AfxMessageBox("查询IDBCreateCommand失败,退出程序");
		return hr;
	}
	hr=pICreateCommand->CreateCommand(NULL,IID_ICommandText,
		(IUnknown **) &pICommandText);
    if(FAILED(hr))
    {
		AfxMessageBox("查询ICommandText失败,退出程序");
		return hr;
	}
	hr=pICommandText->SetCommandText(DBGUID_DBSQL,strCmd);
    if(FAILED(hr))
    {
		AfxMessageBox("ICommandText设置失败,退出程序");
		return hr;
	}
	hr=pICommandText->Execute(NULL,IID_IRowset,NULL,cNumRows,(IUnknown **)pIRowset);
    if(FAILED(hr))
    {
		AfxMessageBox("ICommandText执行失败,退出程序");
		return hr;
	}
	return hr;

}

HRESULT COleDb::Binding(IRowset* pIRowset,IAccessor** pIAccessor,HACCESSOR *hAccessor
						,DBBINDING** ppBindings,ULONG *lNumCols)
{
	ULONG cbColOffset = 0;
	WCHAR* pStringsBuffer;
	IColumnsInfo* pIColumnsInfo;
	DBCOLUMNINFO* pDBColumnInfo;
	HRESULT hr;
	hr=pIRowset->QueryInterface(IID_IColumnsInfo, (void **) &pIColumnsInfo);
    if(FAILED(hr))
    {
		AfxMessageBox("查询IColumnsInfo接口失败,退出程序");
		return hr;
	}
	hr=pIColumnsInfo->GetColumnInfo(lNumCols, &pDBColumnInfo,&pStringsBuffer);
    if(FAILED(hr))
    {
		AfxMessageBox("GetColumnInfo失败,退出程序");
		return hr;
	}
	pIColumnsInfo->Release();
	DBBINDING* pBindings = new DBBINDING[*lNumCols];
	// Using the ColumnInfo Structure, Fill Out the pBindings Array
	for(ULONG i = 0; i < *lNumCols; i++) 
	{
		pBindings[i].iOrdinal = i+1;
		pBindings[i].obValue = cbColOffset;
		pBindings[i].obLength = 0;
		pBindings[i].obStatus = 0;
		pBindings[i].pTypeInfo = NULL;
		pBindings[i].pObject = NULL;
		pBindings[i].pBindExt = NULL;
        pBindings[i].dwPart = DBPART_VALUE;
        pBindings[i].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
        pBindings[i].eParamIO = DBPARAMIO_NOTPARAM; 
        pBindings[i].cbMaxLen = pDBColumnInfo[i].ulColumnSize;
        pBindings[i].dwFlags = 0;
        pBindings[i].wType = pDBColumnInfo[i].wType;
        pBindings[i].bPrecision = pDBColumnInfo[i].bPrecision;
        pBindings[i].bScale = pDBColumnInfo[i].bScale;
        cbColOffset = cbColOffset + pDBColumnInfo[i].ulColumnSize;
	}
	hr=pIRowset->QueryInterface(IID_IAccessor, (void **) pIAccessor);
    if(FAILED(hr))
    {
		AfxMessageBox("查询IAccessor失败,退出程序");
		return hr;
	}
	
/*	ISupportErrorInfo *pErrorInfo;
	hr=m_pAccessor->QueryInterface(IID_ISupportErrorInfo,(void**)&pErrorInfo);
	hr=pErrorInfo->InterfaceSupportsErrorInfo(IID_IAccessor);
	IErrorInfo *pError;*/

	hr=(*pIAccessor)->CreateAccessor(DBACCESSOR_ROWDATA,*lNumCols,pBindings,
		0,hAccessor,NULL);
	*ppBindings=pBindings;
    if(FAILED(hr))
    {
/*		BSTR    pbstrDescription;
		hr=GetErrorInfo(0,&pError);
		pError->GetDescription(&pbstrDescription);
		CString ee=pbstrDescription;
		AfxMessageBox((LPCTSTR)ee);*/
		AfxMessageBox("CreateAccessor出错,退出程序");
		return hr;
	}
	return hr;

}

HRESULT COleDb::GetAllUserData(IRowset* pIRowset,HACCESSOR hAccessor,CObArray *StoreArray)
{
	HRESULT hr;
	ULONG cRowsObtained;
	HROW rghRows[NUMROWS_CHUNK];
	HROW* pRows;
	pRows= &rghRows[0];
	COleDateTime dd;
	dataUserList myUserList;
	CString b;
	int i=0;
	CXhinfo *pOneUser;

	for(;;)
	{
	    hr=pIRowset->GetNextRows(0,0,NUMROWS_CHUNK,&cRowsObtained,&pRows );
		if(cRowsObtained==0) break;
		unsigned int iRow;
		for(iRow=0;iRow<cRowsObtained;iRow++)
		{
			myUserList.User[0]=0;
			myUserList.Pass[0]=0;
			myUserList.begin=0;
			myUserList.end=0;
			pIRowset->GetData(rghRows[iRow], hAccessor, &myUserList);
			pOneUser=new CXhinfo;
			pOneUser->Name=myUserList.User;
			pOneUser->NewName =pOneUser->Name ;
			pOneUser->Pass =myUserList.Pass;
			pOneUser->Begin =myUserList.begin ;
			pOneUser->End =myUserList.end ;
/*
			b=pOneUser->End.Format("%Y/%m/%d");
			b=pOneUser->Pass ;
			AfxMessageBox(b);
*/
			StoreArray->Add(pOneUser);
		}
		hr=pIRowset->ReleaseRows(cRowsObtained, rghRows,NULL,NULL,NULL);
		i+=cRowsObtained;
	}
/*
	b.Format("存入用户为%d",StoreArray->GetSize());
	AfxMessageBox(b);
	b.Format("总共行数为%d",i);
	AfxMessageBox(b);
	AfxMessageBox(pOneUser->Name);
	*pOneUser=*(CXhinfo*)StoreArray->GetAt(3);
	AfxMessageBox(pOneUser->Name);
*/
	return hr;

}

HRESULT COleDb::GetUserTypeData(IRowset* pIRowset,HACCESSOR hAccessor,CObArray *StoreArray)
{
	HRESULT hr;
	ULONG cRowsObtained;
	HROW rghRows[NUMROWS_CHUNK];
	HROW* pRows;
	pRows= &rghRows[0];
	COleDateTime dd;
	dataUserType myUserType;
	CString b,temp;
	int i=0,j=0,p=0,length;
	CXhinfo *pOneUser;
	CTypeValue myType,*pNewType;

	length=StoreArray->GetSize();

	for(;;)
	{
	    hr=pIRowset->GetNextRows(0,0,NUMROWS_CHUNK,&cRowsObtained,&pRows );
		if(cRowsObtained==0) break;
		unsigned int iRow;
		for(iRow=0;iRow<cRowsObtained;iRow++)
		{
			myUserType.Key=-1;
			myUserType.User[0]=0;
			myUserType.begin=0;
			myUserType.end=0;
			pIRowset->GetData(rghRows[iRow], hAccessor, &myUserType);
			for(j=p;j<length;j++)
			{
				pOneUser=(CXhinfo *)StoreArray->GetAt(j);
				temp=myUserType.User;
				if(temp==pOneUser->Name)
				{
					myType.iKey=myUserType.Key;
					myType.Begin = myUserType.begin ;
					myType.End = myUserType.end ;
					pNewType=new CTypeValue;
					*pNewType=myType;
					pOneUser->InfoArray.Add(pNewType);
					break;
				}
			}
/*			pOneUser->Name=myUserList.first;
			pOneUser->NewName =pOneUser->Name ;
			pOneUser->Pass =myUserList.second ;
			pOneUser->Begin =myUserList.begin ;
			pOneUser->End =myUserList.end ;*/
/*
			b=pOneUser->End.Format("%Y/%m/%d");
			b=pOneUser->Pass ;
			AfxMessageBox(b);
*/
//			StoreArray->Add(pOneUser);
		}
		hr=pIRowset->ReleaseRows(cRowsObtained, rghRows,NULL,NULL,NULL);
		i+=cRowsObtained;
	}
/*
	b.Format("存入用户为%d",StoreArray->GetSize());
	AfxMessageBox(b);
	b.Format("总共行数为%d",i);
	AfxMessageBox(b);
	AfxMessageBox(pOneUser->Name);
	*pOneUser=*(CXhinfo*)StoreArray->GetAt(3);
	AfxMessageBox(pOneUser->Name);
*/
	for(int jj=0;jj<length;jj++)
	{
		pOneUser=(CXhinfo *)StoreArray->GetAt(jj);
		int dd=pOneUser->InfoArray.GetSize();
	}
	return hr;

}

HRESULT COleDb::GetTypesList(IRowset* pIRowset,HACCESSOR hAccessor,CObArray *StoreArray)
{
	HRESULT hr;
	ULONG cRowsObtained;
	HROW rghRows[NUMROWS_CHUNK];
	HROW* pRows;
	pRows= &rghRows[0];
	dataTypeInfo myTypeInfo;
	CString b;
	int i=0;
	CTypeInfo *pOneType;

	for(;;)
	{
	    hr=pIRowset->GetNextRows(0,0,NUMROWS_CHUNK,&cRowsObtained,&pRows );
		if(cRowsObtained==0) break;
		unsigned int iRow;
		for(iRow=0;iRow<cRowsObtained;iRow++)
		{
			myTypeInfo.lKey=-1;
			myTypeInfo.TypeEName[0]=0;
			pIRowset->GetData(rghRows[iRow], hAccessor, &myTypeInfo);
			pOneType=new CTypeInfo;
			pOneType->iKey=myTypeInfo.lKey;
			pOneType->sEName=myTypeInfo.TypeEName;
/*
			b=pOneUser->End.Format("%Y/%m/%d");
			b=pOneUser->Pass ;
			AfxMessageBox(b);
*/
			StoreArray->Add(pOneType);
		}
		hr=pIRowset->ReleaseRows(cRowsObtained, rghRows,NULL,NULL,NULL);
		i+=cRowsObtained;
	}
/*
	b.Format("存入用户为%d",StoreArray->GetSize());
	AfxMessageBox(b);
	b.Format("总共行数为%d",i);
	AfxMessageBox(b);
	AfxMessageBox(pOneUser->Name);
	*pOneUser=*(CXhinfo*)StoreArray->GetAt(3);
	AfxMessageBox(pOneUser->Name);
*/
	return hr;

}

⌨️ 快捷键说明

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