- 积分
- 3252
- 明经币
- 个
- 注册时间
- 2001-6-11
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2002-6-21 10:32:00
|
显示全部楼层
ADO(VC++) (MSDN)
ADO 教程 (VC++)
本教程以新版 Microsoft® Visual C++® Extensions 为特征。VC++ Extensions 删除了对繁琐的 VARIANT 数据类型的使用。
本教程还使用了 #import 伪指令,它将 ADO Typelib 转换到头文件中,这个头文件使一些 ADO 的功能在使用和外观上类似于它的同伴 Microsoft® Visual Basic®。
#define INITGUID
#import "c:\Program Files\Common Files\System\ADO\msado15.dll"
no_namespace rename("EOF", "EndOfFile")
#include <stdio.h>
#include "icrsint.h"
void dump_com_error(_com_error &e)
{
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}
class CCustomRs :
public CADORecordBinding
{
BEGIN_ADO_BINDING(CCustomRs)
ADO_VARIABLE_LENGTH_BINDING_ENTRY(1, adVarChar, m_szau_lname,
sizeof(m_szau_lname), lau_lnameStatus, FALSE)
ADO_VARIABLE_LENGTH_BINDING_ENTRY(2, adVarChar, m_szau_fname,
sizeof(m_szau_fname), lau_fnameStatus, FALSE)
ADO_FIXED_LENGTH_BINDING_ENTRY(3, adChar, m_szphone,
sizeof(m_szphone), lphoneStatus, TRUE)
END_ADO_BINDING()
public:
CHAR m_szau_lname[41];
ULONG lau_lnameStatus;
CHAR m_szau_fname[41];
ULONG lau_fnameStatus;
CHAR m_szphone[12];
ULONG lphoneStatus;
};
VOID main()
{
HRESULT hr;
IADORecordBinding *picRs = NULL;
::CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection.1.5");
_RecordsetPtr pRs("ADODB.Recordset.1.5");
CCustomRs rs;
// 步骤 1 — 打开连接
pConn->Open("dsn=pubs;uid=sa;pwd=;");
//步骤 2 — 创建命令
// 步骤 3 — 执行命令
pRs->Open("select * from authors", pConn,
adOpenDynamic, adLockOptimistic, adCmdUnknown);
if (FAILED(hr = pRs->QueryInterface(__uuidof(IADORecordBinding),
(LPVOID*)&picRs)))
_com_issue_error(hr);
if (FAILED(hr = picRs->BindToRecordset(&rs)))
_com_issue_error(hr);
// 步骤 4 — 操作数据
pRs->Fields("au_lname").Optimize = TRUE;
pRs->Sort = "au_lname ASCENDING";
pRs->Filter = "phone LIKE '415 5*";
pRs->MoveFirst();
while (VARIANT_FALSE == pRs->EndOfFile)
{
printf("\a\tName: %s\t %s\tPhone: %s\n",
(lau_fnameStatus == adFldOK ? m_szau_fname : ""),
(lau_lnameStatus == adFldOK ? m_szau_lname): ""),
(lau_lphoneStatus == adFldOK ? m_szphone): ""));
if (lphoneStatus == adFldOK)
lmemcpy(m_szphone, "777", 3);
if (FAILED(hr = picRs->Update(&rs)))
_com_issue_error(hr);
// Change the current row of the Recordset.
// Recordset data for the new current row will automatically be
// extracted and placed in the CCustomRs C++ instance variables.
pRs->MoveNext();
}
pRs->Filter = adFilterNone;
// 步骤 5 — 更新数据
pConn->BeginTrans();
pRs->UpdateBatch();
// 步骤 6-A — 结束更新
pConn->CommitTrans();
}
catch (_com_error &e)
{
dump_com_error(e);
}
catch (ADODB.Error &e)
{
pRS->Filter = adConflictingRecords;
pRs->MoveFirst();
while (VARIANT_FALSE == pRs->EndOfFile)
{
printf("\a\tConflict: Name = %s\t %s\n",
(lau_fnameStatus == adFldOK ? m_szau_fname : ""),
(lau_lnameStatus == adFldOK ? m_szau_lname): ""));
pRs->MoveNext();
}
// 步骤 6-B 部分 — 结束更新
pConn->Rollback();
}
if (picRs)
picRs->Release();
CoUninitialize();
}
VC++ 教程到此结束。 |
|