次の方法で共有


ADO レコードセット XML 永続化の C++ サンプル

この記事では、外部 xml ファイル、ADO オブジェクト、 IStream および XML DOM オブジェクトとの間で ADO Recordset を永続化または読み込む方法について説明します。

元の製品バージョン: ADO レコードセット
元の KB 番号: 262450

概要

ADO 2.5 以降では、Recordset オブジェクトをインターフェイスを実装する任意のオブジェクトに IStream 永続化できます。 この記事のサンプル コードでは、ADO Recordset を外部 XML ファイル、ADO IStream オブジェクト、XML DOM オブジェクトとの間で永続化/読み込む方法を示します。

詳細

  1. このサンプルでは、SQL Server Database の pubs データベースで authors テーブルを使用します。

  2. 接続文字列を変更して、適切なデータソース名とユーザー資格情報を指定します。

  3. 2 つの #import dll があるため、プログラミングを簡単にするために、ADO(ADODB)名前空間の名前を MSXML として変更し、両方の dll で定義されているインターフェイスに MSXML を使用します。 または、これを行わずに、インターフェイスに適切な名前空間のプレフィックスを常に付けることができます。

注:

このコードを実行する前に、 と Password=<strong password> を正しい値に変更User ID=<username>する必要があります。 データベースに対してこの操作を実行するための適切なアクセス許可があることを確認します 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 でコードを実行するには、次のコード行を変更する必要があります。

  • 置換

    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML") rename("EOF", "ADOEOF")
    
    #import "c:\winnt\system32\msxml.dll" by using namespace MSXML;
    

    次の行と置き換えます。

    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML2") rename("EOF", "ADOEOF")
    
    #import "c:\winnt\system32\msxml6.dll" using namespace MSXML2;
    
  • 置換

    pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument));
    

    次の行と置き換えます。

    pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument60));
    

MSXML 6.0 でコードを実行するには、次のコード行を変更する必要があります。

  • 置換

    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML") rename("EOF", "ADOEOF")
    
    #import "c:\winnt\system32\msxml.dll" by using namespace MSXML;
    

    次の行と置き換えます。

    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename_namespace("MSXML2") rename("EOF", "ADOEOF")
    
    #import "c:\winnt\system32\msxml6.dll" using namespace MSXML2;
    
  • 置換

    pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument));
    

    次の行と置き換えます。

    pXMLDOMDoc.CreateInstance(__uuidof(DOMDocument60));