VC ADO使用说明

注意事项:
一,引入msado15.dll
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
如果要发布,可以自带msado15.dll这个文件。
另外,两个rename中间只留一个空格,不能用分号。

二,使用最开始需要初始化COM环境,使用结束需要关闭COM环境
::CoInitialize(NULL); //为应用程序初始化COM环境
CoUninitialize();//清除COM 环境

三,查询,新增,删除,修改等数据库操作,都可以用三种方式:1,命令对象,2,连接对象,3,记录集对象。

四,用记录集修改,增加后,要更新。

五,将结果集显示在datagird上的时候,需要注意:
1,显示前调用m_pConnection->PutCursorLocation(adUseClient);//没有这句会报错“行集合不能作为标签”
2,显示后不能关闭m_pRecordset,否则不显示。
3,显示代码:
m_datagird.SetRefDataSource((LPUNKNOWN)m_pRecordset); //这个会自动将m_pRecordset设置为不析构
m_datagird.Refresh();


============================下面是示例=============================

::CoInitialize(NULL); //为应用程序初始化COM环境
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
//创建与数据库的连接
m_pConnection. CreateInstance(__uuidof(Connection)); //创建连接对象实例
m_pRecordset. CreateInstance(__uuidof(Recordset)); //创建记录集对象实例
m_pCommand. CreateInstance("ADODB.Command"); //创建命令对象实例
try
{
_bstr_t strConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=ACER-05";
m_pConnection->Open(strConnect,"","",adModeUnknown); //打开连接
}
catch(_com_error e) //处理异常
{
AfxMessageBox(e.ErrorMessage());
}

/*
//查询
try
{
CString strSql="select * from Tab";
BSTR bstrSQL=strSql.AllocSysString();
m_pRecordset->Open(bstrSQL, (IDispatch *)m_pConnection,
adOpenDynamic, adLockOptimistic, adCmdText);
int nID;
CString strName;
while(!m_pRecordset->adoEOF)
{
_variant_t theValue;
//获取Col1字段值
theValue=m_pRecordset->Fields->GetItem("Col1")->GetValue();
if(theValue.vt!=VT_NULL)
nID=theValue.iVal;
//获取Col2字段值
theValue=m_pRecordset->GetCollect("Col2");
if(theValue.vt!=VT_NULL)
strName=(char *)_bstr_t(theValue);

CString strField;
strField.Format("The Record %d Values: %s
" , nID, strName);
AfxMessageBox(strField);

m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
*/

/*
//关联记录集到datagird上面。
try
{
m_pConnection->PutCursorLocation(adUseClient);//没有这句会报错“行集合不能作为标签”
CString strSql="select * from Tab";
BSTR bstrSQL=strSql.AllocSysString();
m_pRecordset->Open(bstrSQL, (IDispatch *)m_pConnection,
adOpenDynamic, adLockOptimistic, adCmdText);
m_datagird.SetRefDataSource((LPUNKNOWN)m_pRecordset); //这个会自动将m_pRecordset设置为不析构
m_datagird.Refresh();
//m_pRecordset->Close();//不能关闭,否则不显示。
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
*/

/*
ADO 技术提供了三种添加、修改、删除记录的方法,
一是使用连接对象的Execute 方法,
二是使用命令对象的Execute 方法,
三是使用记录集对象的AddNew、Delete方法。
下面是记录集对象的示例
*/

/*
//添加记录
try
{
CString strSql="select * from Tab";
BSTR bstrSQL=strSql.AllocSysString();
m_pRecordset->Open(bstrSQL, (IDispatch*)m_pConnection,
adOpenDynamic, adLockOptimistic, adCmdText);
//添加记录
m_pRecordset->AddNew();
m_pRecordset->Fields->GetItem("Col1")->Value=(short)22;
m_pRecordset->Fields->GetItem("Col2")->Value=_bstr_t("f");
//更新数据库
m_pRecordset->Update();
m_pRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
*/

/*
//修改记录
try
{
CString strSql="select * from Tab where Col2='f' and Col1=22 ";
BSTR bstrSQL=strSql.AllocSysString();
m_pRecordset->Open(bstrSQL, (IDispatch*)m_pConnection,
adOpenDynamic, adLockOptimistic, adCmdText);
//修改记录的值
m_pRecordset->Fields->GetItem("Col1")->Value=(short)4;
//更新数据库
m_pRecordset->Update();
m_pRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
*/

/*
//用记录集删除记录
try
{
CString strSql="select * from Tab where Col2='f' and Col1=4 ";
BSTR bstrSQL=strSql.AllocSysString();
m_pRecordset->Open(bstrSQL, (IDispatch*)m_pConnection,
adOpenDynamic, adLockOptimistic, adCmdText);
m_pRecordset->Delete(adAffectCurrent);
m_pRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
*/
/*
//用命令对象进行删除
try
{
_variant_t vNULL;
vNULL.vt=VT_ERROR;
vNULL.scode=DISP_E_PARAMNOTFOUND;
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="delete Tab where Col1=3";
m_pCommand->Execute(&vNULL, &vNULL, adCmdText);
//m_pRecordset= m_pCommand->Execute(&vNULL, &vNULL, adCmdText);
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
*/
CoUninitialize();//清除COM 环境
//结束处理
posted @ 2009-12-14 14:13  傲衣华少  阅读(1142)  评论(0编辑  收藏  举报