摘要
使用 ADO 2.5 和更新版本時,資料錄集物件可以保存任何實作 IStream 介面的物件。本文中的程式碼範例會示範如何保存/載入 ADO 資料錄集來進行來回外部 xml 檔、 ADO IStream 物件和 XML DOM 物件。
更多的資訊
1.此範例會使用 pubs 資料庫的 SQL Server 資料庫中 authors 資料表。
2.修改連接字串,提供正確的資料來源名稱和使用者認證。
3.因為兩個 #import dll,為了簡單起見的 programing,我會重新命名 MSXML,ADO (ADODB) 名稱區的連線,並使用 MSXML 這兩個 dll 中定義的介面。或者,您可以永遠的前置詞與適當的命名空間介面未執行此動作。
注意 您必須變更使用者識別碼 = [< 使用者名稱 > 與密碼 < 強式密碼 > = 成正確值,執行這個程式碼。請確認 User ID 具有適當的權限,才能執行此作業,在資料庫上。
// 1. ADO Recordset <-> external xml file
// 2. ADO Recordset <-> ADO IStream Object
// 3. ADO Recordset <-> DOM Document
#import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML") rename("EOF", "ADOEOF")
#import "c:\winnt\system32\msxml.dll"
using namespace MSXML;
#include "stdio.h"
#include "io.h"
void dump_error(_com_error &e) ; //exception handling
void main()
{
HRESULT hr;
CoInitialize(NULL);
try
{
//open the connection, get the reocrdset ready
_ConnectionPtr pConn;
_RecordsetPtr pRs;
hr = pConn.CreateInstance(__uuidof(Connection));
hr = pRs.CreateInstance(__uuidof(Recordset));
pConn->CursorLocation = adUseClient;
_bstr_t strConn("Provider=sqloledb;Data Source=juliaj01;Initial Catalog=pubs;User Id=<username>;Password=<strong password>;");
hr = pConn->Open(strConn, "<username>", "<strong password>", adConnectUnspecified);
hr = pRs->Open("SELECT * from authors", pConn.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
//preparation to save RS as xml file,
struct _finddata_t xml_file;
long hFile;
if( (hFile = _findfirst("authors.xml", &xml_file )) != -1L)
{
DeleteFile("authors.xml"); //if the file exists, delete it
}
// 1. Persist it to an external xml file by calling Save with file name and adPersistXML
hr = pRs->Save("authors.xml", adPersistXML);
// 2. Persist it to ADO IStream Object
_StreamPtrpStream ; //declare one first
pStream.CreateInstance(__uuidof(Stream)); //create it after
hr = pRs->Save(pStream.GetInterfacePtr(), adPersistXML); //old trick, call Save
// 3. Persist it to DOM Document
IXMLDOMDocumentPtr pXMLDOMDoc;
pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument));
hr = pRs->Save(pXMLDOMDoc.GetInterfacePtr(), adPersistXML);
// if you want to check out the content call printf(pXMLDOMDoc->Getxml());
//Recycle the Recordset object
hr = pRs->Close();
// 4. load the recordset back from the file by calling Open with MSPersist provider and adCmdFile.
// the Recordset will be a ReadOnly, Forwardly only
hr = pRs->Open("authors.xml","Provider=MSPersist;",adOpenForwardOnly,adLockReadOnly,adCmdFile);
hr = pRs->Close();
// 5. Load from IStream object, call Open, first param is pStream.GetInterfacePtr()
// Set the steam object position to the beginning of the stream:
pStream->Position = 0;
// call Open, passing in vtMissing for connection string, see Q245485 for details
hr = pRs->Open(pStream.GetInterfacePtr(),vtMissing, adOpenForwardOnly,adLockReadOnly,adCmdFile);
hr = pRs->Close();
// 6 .Load from DOM Document, call Open, first param is pXMLDOMDoc.GetInterfacePtr() and
// pass in vtMissing for connection string, see Q245485
hr = pRs->Open(pXMLDOMDoc.GetInterfacePtr(), vtMissing, adOpenForwardOnly, adLockReadOnly, adCmdFile);
hr = pRs->Close();
//Don't forget to clean up the stream object
hr = pStream->Close();
}
catch(_com_error &e)
{
dump_error(e);
}
}
void dump_error(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
注意本文中所提供的範例程式碼會包含的 MSXML 2.5 或更早版本的參考。如果已經在您的電腦上的取代模式中安裝較新版本的 MSXML,範例程式碼會自動使用這個新版本。如果已經在您的電腦上的 [並排顯示模式中安裝較新版本的 MSXML,則程式碼可能會使用較舊的版本。
若要執行的程式碼,MSXML 6.0 中,下列程式碼行都必須變更:
-
程式碼取代:
rename_namespace("MSXML") #import"C:\Program 使用 Files\System\Ado\msado15.dll"重新命名 ("","ADOEOF"eof) 發
#import"c:\winnt\system32\msxml.dll"使用命名空間 MSXML;
使用:rename_namespace("MSXML2") #import"C:\Program 使用 Files\System\Ado\msado15.dll"重新命名 ("","ADOEOF"eof) 發
#import"c:\winnt\system32\msxml6.dll"使用命名空間 MSXML2; -
程式碼取代:
pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument));
使用:pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument60));
若要執行的程式碼,MSXML 6.0 中,下列程式碼行都必須變更:
-
程式碼取代:
rename_namespace("MSXML") #import"C:\Program 使用 Files\System\Ado\msado15.dll"重新命名 ("","ADOEOF"eof) 發
#import"c:\winnt\system32\msxml.dll"使用命名空間 MSXML;
使用:rename_namespace("MSXML2") #import"C:\Program 使用 Files\System\Ado\msado15.dll"重新命名 ("","ADOEOF"eof) 發
#import"c:\winnt\system32\msxml6.dll"使用命名空間 MSXML2; -
程式碼取代:
pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument));
使用:pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument60));
參考
如需詳細資訊,按一下下面的文件編號,以檢視「Microsoft 知識庫」中的文件:
245485標題作者內容型別語言地區設定版本機密性內容狀態工作群組明發佈日期動作
262447如何保存/載入 OLE DB 資料列集與 XML 使用 ATL OLE DB 消費者樣板類別
259555 PRB: 當您開啟 XML 資料流上的 ADO 資料錄集時,就會發生錯誤