【VC开源代码栏目提醒】:以下是网学会员为您推荐的VC开源代码-ADOWin32ConsoleSaveandOpen.cpp,希望本篇文章对您学习有所帮助。
// ADOWin32ConsoleSaveandOpen.cpp : 定义控制台应用程序的入口点。
//ADO记录集与磁盘文件之间的操作
#include "stdafx.h"
#include <ole2.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
#import "c:\Program Files\Common Files\system\ado\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
//向前函数定义声明
void SaveX1(_RecordsetPtr,_bstr_t);
void SaveX2(_RecordsetPtr);
void SaveX3(_RecordsetPtr,_bstr_t,_ConnectionPtr);
void PrintProviderError(_ConnectionPtr );
void PrintComError(_com_error &e);
void CommonProcessError(_RecordsetPtr ,_com_error &);
//主程序
int _tmain(int argc, _TCHAR* argv[])
{
struct _finddata_t xml_file;
long hFile;
//定义ADO连接对象和记录集对象,缺省的命名空间是ADODB::
_ConnectionPtr pCnn = NULL;
_RecordsetPtr pRstAuthors = NULL;
//定义ADO连接字符串
_bstr_t strCnn("Provider=sqloledb;Data Source=localhost;"
"Initial Catalog=pubs;User Id=sa;Password=mother;");
if(FAILED(::CoInitialize(NULL)))
return 1;
//查找将记录集要保存到特定目录的文件是否存在
if((hFile = _findfirst("E:\\pubs.xml", &xml_file )) != -1L)
{
printf( "File already exists!\n" );
return(true);
}
else
{
SaveX1(pRstAuthors,strCnn);
SaveX2(pRstAuthors);
SaveX3(pRstAuthors,strCnn,pCnn);
}
::CoUninitialize();
return 0;
}
//从SQL Server数据库pubs的authors表中读取数据到ADO记录集,并保存到磁盘文件
void SaveX1(_RecordsetPtr pRstAuthors, _bstr_t strCnn)
{
try
{
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
pRstAuthors->Open("SELECT * FROM authors",strCnn,adOpenDynamic,adLockBatchOptimistic,adCmdText);
//将ADO记录集以XML的格式保存到磁盘文件
pRstAuthors->Save("E:\\pubs.xml",adPersistXML);
pRstAuthors->Close();
}
catch(_com_error &e)
{
CommonProcessError( pRstAuthors,e);
}
}
//从本地XML格式的磁盘文件中读取数据到非连接的ADO记录集中,需要注意的是要实现上述的目的需要在
//本机上有MSPersist provider
void SaveX2(_RecordsetPtr pRstAuthors)
{
try
{
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
//从磁盘XML格式的文件中读取数据到非连接的ADO记录集中
pRstAuthors->Open("E:\\pubs.xml","Provider=MSPersist;",
adOpenForwardOnly,adLockOptimistic,adCmdFile);
//对处于非连接状态的ADO记录集进行必要的操作
pRstAuthors->Find("au_lname = 'Carson'",NULL,adSearchForward);
if(pRstAuthors->EndOfFile)
{
printf("Name not found ...\n");
pRstAuthors->Close();
return;
}
pRstAuthors->GetFields()->GetItem("City")->PutValue("Berkeley");
pRstAuthors->Update();
//将非连接ADO记录集以ADTG保存到本地磁盘文件,这次采用ADTG格式,而不采用
//XML格式主要是为了演示可以将ADO记录集以不同的文件格式进行保存。
pRstAuthors->Save("E:\\pubs.adtg",adPersistADTG);
pRstAuthors->Close();
}
catch(_com_error &e)
{
CommonProcessError( pRstAuthors, e);
}
}
//将更改过的本地磁盘文件中的数据更新到数据库系统中
void SaveX3(_RecordsetPtr pRstAuthors, _bstr_t strCnn,_ConnectionPtr pCnn)
{
try
{
TESTHR(pCnn.CreateInstance(__uuidof(Connection)));
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
pRstAuthors->Open("E:\\pubs.adtg","Provider=MSPersist;",
adOpenForwardOnly,adLockOptimistic,adCmdFile);
pCnn->Open(strCnn,"","",NULL);
pRstAuthors->PutActiveConnection(_variant_t((IDispatch *) pCnn));
pRstAuthors->UpdateBatch(adAffectAll);
pRstAuthors->Close();
pCnn->Close();
}
catch(_com_error &e)
{
CommonProcessError( pRstAuthors,e);
}
}
//从ADO连接对象捕获提供者的错误
void PrintProviderError(_ConnectionPtr pConnection)
{
ErrorPtr pErr = NULL;
if( (pConnection->E