ADO是Microsoft为最新和最强大的数据访问范例 OLE DB 而设计的,是一个便于使用的应用程序层接口。ADO 使您能够编写应用程序以通过 OLE. DB 提供者访问和操作数据库服务器中的数据。OLE DB是较为底层的数据库访问组件,使用起来比较复杂。ADO对OLE DB的接口进行了简化,因此使用起来更加方便。
ADO有3个关键的智能指针类,分别为m_ConnectionPtr, m_RecordsetPtr, m_CommandPtr。m_ConnectionPtr用于标志与数据源的连接;m_RecordsetPtr是数据库记录的集合,用于访问和操纵数据库的记录;m_CommandPtr封装了许多有用的方法,用于执行返回记录集的SQL语句和存储过程。本文主要介绍如何使用m_ConnectionPtr, m_RecordsetPtr对数据库进行增删改查的操作。
想要使用ADO组件,必须导入ADO的库文件。本文使用的是MFC单文件程序框架,需要在头文件stdafx.h中加入如下命令
#import "c:\program files\common files\system\ado\msado15.dll"no_namespaces rename("EOF", "adoEOF") rename("BOF" , "adoBOF")
import的内容是系统安装盘下的msado15.dll文件,根据系统安装盘的不同确定文件的URL。no_namespaces表示程序不使用命名空间。rename("EOF", "adoEOF") 和rename("BOF", "adoBOF")表示对ADO库文件中的EOF和BOF进行重命名,以区别于IO流的EOF和BOF。adoEOF和adoBOF分别表示游标指向记录集第一条记录的上一条记录和记录集最后一条记录的下一条记录。
在建立连接与数据库的连接之前,需要调用::CoInitialize(NULL)先初始化OLE/COM环境。然后创建m_ConnectionPtr对象pConn。调用pConn的Open函数打开与数据源的连接。接着创建m_RecordsetPtr对象pRec,调用pRec的Open函数返回执行SQL语句获取的记录集。再使用pRec的其它成员函数访问记录集的每一条数据。
在数据库中添加、删除和修改记录,一般直接使用m_ConnectionPtr对象的Execute函数。
本程序的控制面板比较简单,因为只是为了练习ADO的使用,如下所示。
1、击”查询“按钮,返回查询的记录,最大的框框中显示记录集中的所有记录的姓名。同时,右侧的框框返回记录集的第一条记录的详细信息;
2、点击”上一条“和”下一条“可以移动游标,以显示上一条和下一条记录;
3、点击”添加“,添加一条记录;
4、点击”保存“,执行update语句,对学号所对应的记录信息进行修改;
5、点击”删除“,删除当前显示的记录;
对应的消息响应函数如下所示。
查询响应:
void CTest_adov3Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
::CoInitialize(NULL);
if(FAILED(pConn.CreateInstance(__uuidof(Connection))))
{
AfxMessageBox("Failed create connection.");
return;
}
_bstr_t strConnect="Provider=SQLOLEDB;Server=(local);\
Database=lsc; uid=sa;Password=lsc;";
if(FAILED(pConn->Open(strConnect, "", "", adModeUnknown)))
{
AfxMessageBox("Failed open.");
return;
}
if(FAILED(pRec.CreateInstance(__uuidof(Recordset))))
{
AfxMessageBox("Failed create Recordset.");
return;
}
if(FAILED(pRec->Open("SELECT * FROM seu order by student",pConn.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText)))
{
AfxMessageBox("Failed open Recordset.");
return;
}
if(!pRec->adoEOF)
{
_bstr_t str1=pRec->GetCollect("studentID");
_bstr_t str2=pRec->GetCollect("student");
_bstr_t str3=pRec->GetCollect("sex");
_bstr_t str4=pRec->GetCollect("home");
SetDlgItemText(IDC_STUID, str1);
SetDlgItemText(IDC_STUNAME, str2);
SetDlgItemText(IDC_SEX, str3);
SetDlgItemText(IDC_HOME, str4);
}
while(!pRec->adoEOF)
{
_bstr_t str=pRec->GetCollect("student");
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(str);
pRec->MoveNext();
}
while(!pRec->adoBOF)
pRec->MovePrevious();
pRec->MoveNext();
/*_variant_t RecordAffected;//create table users(ID integer, username text, age integer, birthday datetime)
if(FAILED(pConn->Execute("select * from seu order by student", \
&RecordAffected, adCmdText)))
{
AfxMessageBox("Failed create table.");
return;
}*/
}
上一条响应:
void CTest_adov3Dlg::OnLast()
{
// TODO: Add your control notification handler code here
if(!pRec->adoBOF)
{
pRec->MovePrevious();
}
if(!pRec->adoBOF)
{
//MessageBox("可以往前");
SetDlgItemText(IDC_STUID, "");
SetDlgItemText(IDC_STUNAME, "");
SetDlgItemText(IDC_SEX, "");
SetDlgItemText(IDC_HOME, "");
_bstr_t str1=pRec->GetCollect("studentID");
_bstr_t str2=pRec->GetCollect("student");
_bstr_t str3=pRec->GetCollect("sex");
_bstr_t str4=pRec->GetCollect("home");
SetDlgItemText(IDC_STUID, str1);
SetDlgItemText(IDC_STUNAME, str2);
SetDlgItemText(IDC_SEX, str3);
SetDlgItemText(IDC_HOME, str4);
}
else
{
pRec->MoveNext();
}
}
下一条响应:
void CTest_adov3Dlg::OnNext()
{
// TODO: Add your control notification handler code here
if(!pRec->adoEOF)
{
pRec->MoveNext();
}
else
{
MessageBox("不能往后");
}
if(!pRec->adoEOF)
{
//MessageBox("可以往后");
SetDlgItemText(IDC_STUID, "");
SetDlgItemText(IDC_STUNAME, "");
SetDlgItemText(IDC_SEX, "");
SetDlgItemText(IDC_HOME, "");
_bstr_t str1=pRec->GetCollect("studentID");
_bstr_t str2=pRec->GetCollect("student");
_bstr_t str3=pRec->GetCollect("sex");
_bstr_t str4=pRec->GetCollect("home");
SetDlgItemText(IDC_STUID, str1);
SetDlgItemText(IDC_STUNAME, str2);
SetDlgItemText(IDC_SEX, str3);
SetDlgItemText(IDC_HOME, str4);
}
else
{
pRec->MovePrevious();
}
}
添加响应:
void CTest_adov3Dlg::OnAdd()
{
// TODO: Add your control notification handler code here
CString str1;
GetDlgItemText(IDC_STUID, str1);
CString str2;
GetDlgItemText(IDC_STUNAME, str2);
CString str3;
GetDlgItemText(IDC_SEX, str3);
CString str4;
GetDlgItemText(IDC_HOME, str4);
CString strSQL;
strSQL="insert into seu values('";
strSQL+=str1;
strSQL+="','";
strSQL+=str2;
strSQL+="','";
strSQL+=str3;
strSQL+="','";
strSQL+=str4;
strSQL+="')";
_bstr_t bstrSQL(strSQL);
//MessageBox(bstrSQL);
_variant_t RecordAffected;
//pConn->Execute(bstrSQL, &RecordAffected, adCmdText);
if(FAILED(pConn->Execute(bstrSQL, &RecordAffected, adCmdText)))
{
MessageBox("添加失败");
}
}
保存响应:
void CTest_adov3Dlg::OnSave()
{
// TODO: Add your control notification handler code here
CString str1;
GetDlgItemText(IDC_STUID, str1);
CString str2;
GetDlgItemText(IDC_STUNAME, str2);
CString str3;
GetDlgItemText(IDC_SEX, str3);
CString str4;
GetDlgItemText(IDC_HOME, str4);
CString strSQL;
strSQL="update seu set student='";
strSQL+=str2;
strSQL+="', sex='";
strSQL+=str3;
strSQL+="', home='";
strSQL+=str4;
strSQL+="' where studentID='";
strSQL+=str1;
strSQL+="'";
_bstr_t bstrSQL(strSQL);
//MessageBox(bstrSQL);
_variant_t RecordAffected;
//pConn->Execute(bstrSQL, &RecordAffected, adCmdText);
if(FAILED(pConn->Execute(bstrSQL, &RecordAffected, adCmdText)))
{
MessageBox("修改失败");
}
}
删除响应:
void CTest_adov3Dlg::OnDelete()
{
// TODO: Add your control notification handler code here
CString str1;
GetDlgItemText(IDC_STUID, str1);
CString str2;
CString strSQL;
strSQL="delete from seu where studentID='";
strSQL+=str1;
strSQL+="'";
_bstr_t bstrSQL(strSQL);
//MessageBox(bstrSQL);
_variant_t RecordAffected;
//pConn->Execute(bstrSQL, &RecordAffected, adCmdText);
if(FAILED(pConn->Execute(bstrSQL, &RecordAffected, adCmdText)))
{
MessageBox("删除失败");
}
}