1:导入环境
stdafx.h
#import "C:\\Program Files\\Common Files\\System\\ADO\\msado15.dll" rename_namespace("ADOCG") rename("EOF","adoEOF") //rename("BOF","adoBOF") no_namespace
using namespace ADOCG;
_ConnectionPtr m_ptrConnection;//数据库对象
_RecordsetPtr m_ptrRecordset; //声明记录集指针
m_ptrConnection.CreateInstance("ADODB.Connection");
try
{
m_ptrConnection->ConnectionTimeout = 3;
//连接ACCESS2000
CString strConnect;
strConnect=_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=")+_T("C:\\TableData.mdb");
_bstr_t bstrsql;
bstrsql=(_bstr_t)strConnect;
m_ptrConnection->Open(bstrsql,"","",adModeUnknown);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format(_T("连接数据库失败!rn错误信息:%s"),e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
2:读取数据库
将整张表显示在GridCtrl控件中
try
{
_RecordsetPtr m_ptrRecordset; //声明记录集指针
m_ptrRecordset.CreateInstance(__uuidof(Recordset)); //创建实例
//读取数据库:
_bstr_t bstrsql;
bstrsql="select * from TableData";
HRESULT hr = m_ptrRecordset->Open(bstrsql, m_ptrConnection.GetInterfacePtr(),adOpenDynamic, adLockPessimistic, adCmdText);
Fields* fields=NULL;
long countl;
BSTR bstr;
m_ptrRecordset->get_Fields(&fields);
countl = fields->Count;
MaxCol=countl;
int cnt=0;
for(long i=0;i<countl;i++)
{
fields->Item[i]->get_Name(&bstr);
CString colname=(CString)bstr;
if (colname==ColNames.GetAt(i))
{
m_Grid.InsertColumn(i,colname,LVCFMT_LEFT,100,0);
}
else
{
m_Grid.InsertColumn(i,colname,LVCFMT_LEFT,1,0);
}
cnt++;
}
fields->Release();
showCols=cnt;
int j=0;
while(!m_ptrRecordset->adoEOF)
{
//循环一直读取每一行数据
m_Grid.InsertItem(j,0);
for (int i=0;i<cnt;i++)
{
LVCOLUMN lvcol;
TCHAR lpBuffer[256];
lvcol.mask = LVCF_TEXT|LVCF_SUBITEM;
lvcol.pszText =lpBuffer;
lvcol.cchTextMax = 256;
m_Grid.GetColumn(i, &lvcol);
CString strName= lvcol.pszText;
_variant_t var = m_ptrRecordset->GetCollect((_bstr_t)strName);
if(var.vt != VT_NULL)
{
strName = (LPCSTR)_bstr_t(var);
}
else
{
strName=_T("");
}
m_Grid.SetItemText(j,i,strName);
}
j++;
m_ptrRecordset->MoveNext();/// 移到下一条记录
}
if (m_ptrRecordset!=NULL)
{
m_ptrRecordset->Close();
m_ptrRecordset=NULL;
}
}
catch (_com_error* e)
{
AfxMessageBox(e->Description());
}
3:删除数据库
_bstr_t bstrsql="delete from TableData";
try
{
_variant_t RecordsAffected;
m_ptrConnection->Execute(bstrsql,&RecordsAffected,adCmdText);
}
catch (_com_error* e)
{
AfxMessageBox(e->Description());
}
4:新增数据
方法1:
bstrsql="select * from TableData";
m_ptrRecordset.CreateInstance(__uuidof(Recordset)); //创建记录集对象实例
m_ptrRecordset->Open(bstrsql, m_ptrConnection.GetInterfacePtr(),adOpenDynamic, adLockPessimistic, adCmdText);
try
{
m_ptrRecordset->AddNew(); //添加新行
//向数据库中插入数据
CString strKey;
CString strText;
//ID
strKey=_T("ID");
strText=_T("1");
m_ptrRecordset->PutCollect(( _variant_t)strKey,(_variant_t)strText);
//Name
strKey=_T("Name");
strText=_T("xiaoli");
m_ptrRecordset->PutCollect(( _variant_t)strKey,(_variant_t)strText);
//Length
strKey=_T("Age");
strText=_T("20");
m_ptrRecordset->PutCollect(( _variant_t)strKey,(_variant_t)strText);
//Image1
strKey=_T("Image1");
strText=m_strPicturePath1;
m_ptrRecordset->PutCollect(( _variant_t)strKey,(_variant_t)strText);
//Image2
strKey=_T("Image2");
strText=m_strPicturePath2;
m_ptrRecordset->PutCollect(( _variant_t)strKey,(_variant_t)strText);
m_ptrRecordset->Update(); //更新数据表记录
if (m_ptrRecordset!=NULL)
{
m_ptrRecordset->Close(); //关闭记录集
m_ptrRecordset=NULL;
}
}
catch (...) //捕捉可能出现的错误
{
AfxMessageBox(_T("操作失败")); //弹出错误提示
return;
}
方法2:
CString sql;
sql = ("insert into TableData(ID, Name,Age,Image1,Image2) values('2','XiaoMei','22','C:\\123.bmp','C:\\1234.bmp') ; //添加变量到数据库
try
{
m_pConnection->Execute((_bstr_t)sql,NULL, adCmdText);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}