www.pudn.com > TibetanWordManage.rar > QkyAdo.cpp
#include "stdafx.h"
#include "QkyAdo.h"
CQkyAdo::CQkyAdo()
{
::CoInitialize(NULL);
bConnection = FALSE;
m_pConnection = NULL;
m_pRecordset = NULL;
}
CQkyAdo::CQkyAdo(CString &strFileName, CString &strTableName)
{
::CoInitialize(NULL);
bConnection = FALSE;
m_pConnection = NULL;
m_pRecordset = NULL;
}
CQkyAdo::~CQkyAdo()
{
if( bConnection == TRUE)
{
if(m_pRecordset->State)
m_pRecordset->Close();
if( m_pConnection->State)
m_pConnection->Close();
}
// 关闭记录集
m_pConnection= NULL;
m_pRecordset = NULL;
::CoUninitialize();
}
// 打开本地Access库的连接
BOOL CQkyAdo::OpenConnection(CString strFileName)
{
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些想不到的错误。
// 连接语句: "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = Demo.mdb"
//如果已经连接,不需重新打开
if(bConnection == TRUE)
{
if(m_pConnection !=NULL && m_pConnection->State != adStateClosed)
return TRUE;
}
CString str2000 = "Provider=Microsoft.Jet.OLEDB.4.0;"; //针对ACCESS2000环境
CString str97 = "Provider=Microsoft.Jet.OLEDB.3.51;"; //针对ACCESS97环境
CString strFrom = "Data Source=";
CString strString;
strString.Format("%s%s%s", str2000, strFrom, strFileName);
_bstr_t strCon = _bstr_t( (LPCTSTR)strString );
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
if(m_pConnection == NULL) return FALSE;
HRESULT hr = m_pConnection->Open(strCon,"","",adModeUnknown);
//m_pConnection->Open(
if(FAILED(hr))
return FALSE;
bConnection = TRUE;
return TRUE;
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库是否在当前路径下!");
return FALSE;
}
return FALSE;
}
//创建数据表
BOOL CQkyAdo::CreateTable(CString strSQL)
{
//SQL语言格式 == "CREATE TABLE users(ID INTEGER,username TEXT,old INTEGER,birthday DATETIME)"
//没有连接数据源
if(bConnection == FALSE)
{
if(m_pConnection ==NULL && m_pConnection->State == TRUE)
AfxMessageBox("数据源没有连接!");
return FALSE;
}
_variant_t RecordsAffected;
_bstr_t str((LPCTSTR)strSQL);
m_pConnection->Execute(str, &RecordsAffected, adCmdText);
return TRUE;
}
//打开数据记录集中指定的表// 使用ADO创建数据库记录集
BOOL CQkyAdo::OpenRecordset(CString strTableName)
{
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些想不到的错误。
if((m_pConnection == NULL)|| m_pConnection->State==adStateClosed)
{
AfxMessageBox("未连接数据库源");
return FALSE;
}
//字符串转化
//SQL 语句 = "SELECT * FROM TableName"
CString s("SELECT * FROM "); //SQL语句
CString strTemp;
strTemp = s + strTableName;
_variant_t str(strTemp);
try
{
HRESULT hr = m_pRecordset.CreateInstance(__uuidof(Recordset));
if(FAILED(hr))return FALSE;//建立实例失败
m_pRecordset->CursorType = adOpenDynamic; //动态游标
m_pRecordset->CursorLocation = adUseClient;
m_pRecordset->LockType = adLockPessimistic; // 最安全的数据锁定方式
m_pRecordset->Sort = "TIBETWORD ASC"; // ASC==顺序;DESC == 倒序
m_pRecordset->Open( str, // 打开指定表
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
return TRUE;
}
//判断是否已经连接数据源并打开指定表
BOOL CQkyAdo::IsOpen()
{
if ( (m_pConnection !=NULL && m_pConnection->State == TRUE) &&
(m_pRecordset != NULL && m_pRecordset->State == TRUE) )
return TRUE;
else
return FALSE ;
}
BOOL CQkyAdo::AddNew()
{
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些想不到的错误。
try
{
m_pRecordset->AddNew();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
return TRUE;
}
BOOL CQkyAdo::SetValue(CString strItem, CString strValue)
{
try
{
m_pRecordset->PutCollect(_variant_t(strItem), _variant_t(strValue));
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
return TRUE;
}
BOOL CQkyAdo::Update()
{
try
{
m_pRecordset->Update();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
return TRUE;
}
//修改指定索引的字段项
//移动指针到制定位置
BOOL CQkyAdo::EditItem(long Index)
{
m_pRecordset->MoveFirst();
m_pRecordset->Move(Index, _variant_t((long)adBookmarkFirst));
return TRUE;
}
//查找制定的记录项在数据库中的索引位置
long CQkyAdo::FindItem(CString strItem, CString strValue)
{
long count =0;
_variant_t var;
CString strName;
try
{
long size = m_pRecordset->GetRecordCount(); //如果没有记录
if(size ==0 || size == -1) return -1;
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect(_variant_t(strItem));
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
if(strName.Compare((LPCTSTR)strValue) ==0 )
{
return count;
}
count++;
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
return -1;
}
//查找一个词在库中的具体位置
long CQkyAdo::FindItem(CString strWord)
{
CString str;
str.Format("TIBETWORD LIKE '%s'", strWord);
try
{
long size = m_pRecordset->GetRecordCount(); //如果没有记录
if( size < 1) return -1;
m_pRecordset->MoveFirst();
m_pRecordset->Find((LPCTSTR)str, 0, adSearchForward, "");
if( ! m_pRecordset->adoEOF )
{
return (long) m_pRecordset->AbsolutePosition;
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
return -1;
}
//查找指定的字段
BOOL CQkyAdo::FindWord(CString strWord, CString &strExplain)
{
CString str;
str.Format("TIBETWORD LIKE '%s'", strWord);
try
{
long size = m_pRecordset->GetRecordCount(); //如果没有记录
if( size < 1) return FALSE;
m_pRecordset->MoveFirst();
m_pRecordset->Find((LPCTSTR)str, 0, adSearchForward, "");
if( ! m_pRecordset->adoEOF )
{
_variant_t var1 = m_pRecordset->GetCollect(_variant_t("TIBETWORD"));
_variant_t var2 = m_pRecordset->GetCollect(_variant_t("EXPLAIN"));
if(var1.vt != VT_NULL && var2.vt != VT_NULL)
{
CString strT = (LPCSTR)_bstr_t(var1);
CString strE = (LPCSTR)_bstr_t(var2);
strExplain = strE;
}
return TRUE;
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
return FALSE;
}
////////////////////////////////
//
// 将所有词条装入ListBox
//
////////////////////////////////
void CQkyAdo::LoadToList(CListBox * pList)
{
_variant_t var;
try
{
long size = m_pRecordset->GetRecordCount(); //如果没有记录
if( size < 1) return;
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect(_variant_t("TIBETWORD"));
if(var.vt != VT_NULL)
pList->AddString((LPCTSTR)_bstr_t(var)) ;
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
BOOL CQkyAdo::DeleteWord(CString strWord)
{
CString str;
str.Format("TIBETWORD LIKE '%s'", strWord);
try
{
long size = m_pRecordset->GetRecordCount(); //如果没有记录
if( size < 1) return FALSE;
m_pRecordset->MoveFirst();
m_pRecordset->Find((LPCTSTR)str, 0, adSearchForward, "");
if( ! m_pRecordset->adoEOF )
{
m_pRecordset->Delete(adAffectCurrent);
m_pRecordset->Update();
return TRUE;
}
}
catch(_com_error *e)
{AfxMessageBox(e->ErrorMessage());}
return FALSE;
}
//装载数据
long CQkyAdo::LoadWord(LPCTSTR lpBuff, CProgressCtrl* lpProg)
{
CString strBuff(lpBuff);
long i=0, first=2, end=0;
long len = strBuff.GetLength();
BYTE b = 0x00;
CString strWord, strExplain;
BOOL bWord= FALSE, bExplain= FALSE;
long count =0, num=0;
for(i=2;i 0x80) i++;
else
{
if( b==0x7c ) // 0x7c = |
{
end = i;
char szStr[1024] = {'\0'};
if(end-first > 1)
{
memcpy((void *)szStr, lpBuff+first, end-first);
strWord.Format("%s", szStr);
bWord = TRUE;
}
first = i+1;
}
if( b == 0x23) // 0x23 == #
{
end = i-1;
char szStr[1024] = {'\0'};
if(end-first > 1)
{
memcpy((void *)szStr, lpBuff+first, end-first);
strExplain.Format("%s", szStr);
bExplain = TRUE;
}
first = i+1;
}
if(bWord == TRUE && bExplain == TRUE)
{
bWord= FALSE;
bExplain= FALSE;
if( FindItem(CString("TIBETWORD"), strWord) == -1 )
{
try
{
m_pRecordset->AddNew();
m_pRecordset->PutCollect(_variant_t("TIBETWORD"), _variant_t(strWord));
m_pRecordset->PutCollect(_variant_t("EXPLAIN"), _variant_t(strExplain));
m_pRecordset->Update();
num ++;
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
lpProg->SetPos(count);
count++;
strWord.Empty();
strExplain.Empty();
}
}
}
return num;
}
///导出数据
BOOL CQkyAdo::OutWord(CFile * lpFile, CProgressCtrl* lpProg)
{
_variant_t var1;
_variant_t var2;
CString strItem, strWord, strExplain;
try
{
long i=1, size = m_pRecordset->GetRecordCount(); //如果没有记录
lpProg->SetRange32(0, size);
lpProg->SetStep(1);
lpProg->SetPos(0);
if( size < 1) return FALSE;
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
var1 = m_pRecordset->GetCollect(_variant_t("TIBETWORD"));
var2 = m_pRecordset->GetCollect(_variant_t("EXPLAIN"));
if(var1.vt != VT_NULL && var2.vt != VT_NULL)
strWord = (LPCSTR)_bstr_t(var1);
strExplain = (LPCSTR)_bstr_t(var2);
strItem = strWord + "|" + strExplain + "#";
lpFile->Write((void*) (LPCTSTR) strItem, strItem.GetLength() );
lpProg->SetPos(i);
i++;
m_pRecordset->MoveNext();
}
lpProg->SetPos(0);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
return TRUE;
}