Ado recordset is saved as XML, or as a string, and saved as stream. Official MS Method

Source: Internet
Author: User

# Import "C:/program files/common files/system/ADO/msado15.dll" rename_namespace ("MSXML") Rename ("EOF", "adoeof ")

# Import "C:/Windows/system32/MSXML. dll"
Using namespace MSXML;
Using namespace STD;

# 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 = 127.0.0.1; initial catalog = pubs; user id = fan; Password = 123456 ;");
HR = pconn-> open (strconn, "fan", "123456", adconnectunspecified );
HR = PRS-> open ("select * from authors", pconn. getinterfaceptr (), adopenforwardonly, adlockreadonly, ad1_text );

// Preparation to save RS as XML file,
Struct _ finddata_t xml_file;
Long hfile;
If (hfile = _ findfirst ("authors. xml", & xml_file ))! =-1l)
{
Deletefile (_ T ("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

_ Streamptr pstream; // 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
MSXML: ixmldomdocumentptr pxmldomdoc;
Pxmldomdoc. createinstance (_ uuidof (MSXML: domdocument ));
HR = PRS-> Save (pxmldomdoc. getinterfaceptr (), adpersistxml );

// Add content to me

// Put the content in pxmldomdoc into a string, which can be replaced by cstring.

Char * STR = pxmldomdoc-> getxml ();

// If you wato 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 ad1_file.
// The recordset will be a readonly, forwardly only
HR = PRS-> open ("authors. xml", "provider = mspersist;", adopenforwardonly, adlockreadonly, ad1_file );
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, ad1_file );

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, ad1_file );

HR = PRS-> close ();

 

// Don't forget to clean up the stream object
HR = pstream-> close ();

}
Catch (_ com_error & E)
{
Dump_error (E );
}

Getchar ();
}

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 );

}

 

Link: http://support.microsoft.com/kb/262450/zh-cn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.