现在我们来介绍用ADO方式来连接access数据库
首先我们要导入#import "c:/program files/common files/system/ado/msado15.dll" /no_namespace /rename ("EOF", "adoEOF")其中路径名可以根据自己系统安装的ADO支持文件的路径来自行设定。
其次,在程序初始过程中需要初始化组件一般可以用AfxOleInit();这里我们要注意一下ADO的操 作过程中主要的功能组件有以下三个:
一是:_ConnectionPtr:智能指针,通常用于打开、关闭一个库连接或用它的Execute方法来执行一个不返回结果的命令语句打开一个库连接。先创建一个实例指针,再用Open打开一个库连接,它将返回一个IUnknown的自动化接口指针。代码如下所示:
_ConnectionPtr self_Connection;
// 初始化COM,创建ADO连接等操作
AfxOleInit();
self_Connection.CreateInstance(__uuidof(Connection));//异常处理
try
{
// 打开本地Access库data文件夹中的test.mdb
self_Connection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data/test.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e->ErrorMessage());
return false;
} //关闭一个库连接。如果连接状态有效,则用Close方法关闭它并赋于它空值。
if(self_Connection->State)
self_Connection->Close();
self_Connection= NULL;
二是:_RecordsetPtr智能指针,可以用来打开库内数据表,并可以对表内的记录、字段等进行各种操作。
——打开数据表。打开库内表名为infor的数据表,用sql语句代码如下:
_RecordsetPtr self_Recordset;
self_Recordset.CreateInstance(__uuidof(Recordset));try
{
self_Recordset->Open("SELECT * FROM infor", // 查询infor表中所有字段
theApp.self_Connection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
操纵方法 ——读取表内数据。将表内数据全部读出如果没有遇到表结束标志adoEOF,则用GetCollect(字段名)或self_Recordset->Fields->GetItem(字段名)->Value方法,来获取当前记录指针所指的字段值,然后再用MoveNext()方法移动到下一条记录位置。代码如下所示:
_variant_t var;
CString strChinese,strEnglish
try
{
if(!self_Recordset->BOF)
self_Recordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return;
} // 读入库中各字段并加入列表框中
while(!self_Recordset->adoEOF)
{
var = self_Recordset->GetCollect("chinese");
if(var.vt != VT_NULL)
strChinese = (LPCSTR)_bstr_t(var);
var = self_Recordset->GetCollect("english");
if(var.vt != VT_NULL)
strEnglish = (LPCSTR)_bstr_t(var); AfxMessageBox(strEnglish+"的中文意思是: "+strChinese);
self_Recordset->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} ——插入记录。可以先用AddNew()方法新增一个空记录,再用PutCollect(字段名,值)输入每个字段的值,最后再Update()更新到库中数据既可。其中变量m_Name和m_Age分别为姓名及年龄编辑框的成员变量名。代码所下所示:
try
{
// 写入各字段值
self_Recordset->AddNew();
self_Recordset->PutCollect("Name", _variant_t(m_Name));
self_Recordset->PutCollect("Age", atol(m_Age));
self_Recordset->Update(); AfxMessageBox("插入成功!");
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
操纵方法——移动记录指针。移动记录指针可以通过MoveFirst()方法移动到第一条记录、MoveLast()方法移动到最后一条记录、MovePrevious()方法移动到当前记录的前一条记录、MoveNext()方法移动到当前记录的下一条记录。但我们有时经常需要随意移动记录指针到任意记录位置时,可以使用Move(记录号)方法来实现,注意: Move()方法是相对于当前记录来移动指针位置的,正值向后移动、负值向前移动,如:Move(1),当前记录是1时,它将从记录1开始往后再移动1条记录位置。代码如下所示:
try
{
// 先将指针移向第一条记录,然后就可以相对第一条记录来随意移动记录指针
self_Recordset->MoveFirst();
self_Recordset->Move(1);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
操纵方法——修改记录中字段值。可以将记录指针移动到要修改记录的位置处,直接用PutCollect(字段名,值)将新值写入并Update()更新数据库既可。可以用上面方法移动记录指针,修改字段值代码如下所示:
try
{
// 假设对第二条记录进行修改
self_Recordset->MoveFirst();
self_Recordset->Move(1); // 从0开始
self_Recordset->PutCollect("chinese", _variant_t(m_Chinese));
self_Recordset->PutCollect("english", atol(m_English));
self_Recordset->Update();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
操纵方法——删除记录。删除记录和上面修改记录的操作类似,先将记录指针移动到要修改记录的位置,直接用Delete()方法删除它并用Update()来更新数据库既可。代码如下所示:
try
{
// 假设删除第二条记录
self_Recordset->MoveFirst();
self_Recordset->Move(1); // 从0开始
self_Recordset->Delete(2); //
self_Recordset->Update();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
} ——关闭记录集。直接用Close方法关闭记录集并赋于其空值。代码如下所示: self_Recordset->Close();
self_Recordset = NULL;
三是:CommandPtr智能指针,可以使用_ConnectionPtr或_RecordsetPtr来执行任务,定义输出参数,执行存储过程或SQL语句。 先创建一个_CommandPtr实例指针,再将库连接和SQL语句做为参数,执行Execute()方法既可。代码如下所示: _CommandPtr self_Command;
self_Command.CreateInstance(__uuidof(Command));
self_Command->ActiveConnection = self_Connection; // 将库连接赋于它
self_Command->CommandText = "SELECT * FROM infor"; // SQL语句
self_Recordset = self_Command->Execute(NULL, NULL,adCmdText); // 执行SQL语句,返回记录集