Pull operations of SQL Server CE

Source: Internet
Author: User
Extracted from the EVC advanced programming and application development book, we can use hresult hR = noerror;
Isscerda * pcerda = NULL;
BSTR = NULL;
BSTR bconnectstr = NULL;
BSTR bquerystr = NULL;
BSTR blocalconnectstr = NULL;
BSTR blocaltablename = NULL;
BSTR berrortablename = NULL;
Rda_trackoption trackoption = trackingoff; HR = coinitializeex (null, coinit_multithreaded); // You must initialize the COM interface first.
HR = cocreateinstance (clsid_remotedataaccess, null, clsctx_inproc_server, iid_isscerda, (lpvoid *) & pcerda );
Assert (hR = s_ OK );
Assert (pcerda! = NULL );
 
// Set the Internet URL
BSTR = sysallocstring (_ T ("http: // host address/virtual path/sscesaw.dll"); // The host address must use an IP address instead of a computer name.
Pcerda-> put_interneturl (BSTR );
Sysfreestring (BSTR); // set Internet Username
BSTR = sysallocstring (_ T (""));
Pcerda-> put_internetlogin (BSTR );
Sysfreestring (BSTR); // sets the internet password
BSTR = sysallocstring (_ T (""));
Pcerda-> put_internetpassword (BSTR );
Sysfreestring (BSTR); trackoption = trackingoff; // set the name of the error table.
Berrortablename = sysallocstring (_ T ("tblerrors "));

// Set the server connection string
Bconnectstr = sysallocstring (_ T ("provider = sqloledb; Data Source = Host address; initial catalog = database name; user id = sa; Password = 123 ")); // It is best to set SQL Server to the mode of SQL Server and Windows mutual authentication and set a password
 
// Set the connection string of the local database
Blocalconnectstr = sysallocstring (_ T ("provider = Microsoft. sqlserver. oledb. ce.2.0; Data Source = // database name on my documents // PDA. SDF "); // The first-mover event first creates an empty database on isqlw20.exe installed on PDA. Do not create a table; otherwise, an error occurs. It is recommended that you check whether the suffix SDF exists after creation.
Pcerda-> put_localconnectionstring (blocalconnectstr );
Sysfreestring (BSTR );

// Set the local table name
Blocaltablename = sysallocstring (local table name); // This table must not exist beforehand. Otherwise, an error occurs.

// 9. Set the query SQL statement
Bquerystr = sysallocstring (_ T ("select * from table name on SQL Server "));
// Obtain server data
HR = pcerda-> pull (blocaltablename, bquerystr, bconnectstr, trackoption, berrortablename );

Sysfreestring (blocaltablename );
Sysfreestring (bquerystr );
Sysfreestring (bconnectstr );
Sysfreestring (berrortablename );

If (succeeded (HR ))
{
Afxmessagebox (_ T ("OK "));
}
Else
{
Issceerrors * pissceerrors = NULL;
If (succeeded (hR = pcerda-> get_errorrecords (& pissceerrors )))
{
Showerrors (pissceerrors); // This is an easy-to-find error, which is listed below
Pissceerrors-> release ();
}
}
// Release the remote access interface
 
Pcerda-> release (); void showerrors (issceerrors * pissceerrors)
{
Hresult hr;
Long cbbuf;
Long I;
Long lerrorcount;
Long lerrorindex;
Long lparamcount;
Long lparamindex;
Variant var;
Variant varparam;
Wchar wszbuff [4096];
Wchar * pwszbuffpos = & wszbuff [0];
BSTR;
Issceerror * pissceerror = NULL;
Issceparams * pissceparams = NULL;
Issceparam * pissceparam = NULL;
Bool fsuccess = false;
 
// Initialize the variable
Variantinit (& var );
Variantinit (& varparam );
 
// Obtain the number of errors through the iissceerrors Interface
If (failed (hR = pissceerrors-> get_count (& lerrorcount )))
Goto exit;
If (lerrorcount <= 0)
{
: MessageBox (null, l "no error message", l "Display error", mb_ OK );
Fsuccess = true;
Goto exit;
}
 
// Use the pop-up dialog box to display each error.
For (lerrorindex = 0; lerrorindex <lerrorcount; lerrorindex ++)
{
Cbbuf = swprintf (pwszbuffpos, l "error % d of % d/R/N ",
Lerrorindex + 1, lerrorcount );
Pwszbuffpos + = cbbuf;

// Get error records
Var. Vt = vt_i4;
Var. lval = lerrorindex;
If (failed (hR = pissceerrors-> get_item (VAR, & pissceerror) goto exit;

// Get error details
If (failed (hR = pissceerror-> get_description (& BSTR) goto exit;
Cbbuf = swprintf (pwszbuffpos, l "Description: '% s'/R/N", BSTR );
Pwszbuffpos + = cbbuf;
Sysfreestring (BSTR );

// Error message number
If (failed (hR = pissceerror-> get_number (& I) goto exit;
Cbbuf = swprintf (pwszbuffpos, l "number: % 8.8x/R/N", I );
Pwszbuffpos + = cbbuf;

// Local error
If (failed (hR = pissceerror-> get_nativeerror (& I) goto exit;
Cbbuf = swprintf (pwszbuffpos, l "native_error: % d/R/N", I );
Pwszbuffpos + = cbbuf;

// Get the error source
If (failed (hR = pissceerror-> get_source (& BSTR) goto exit;
Cbbuf = swprintf (pwszbuffpos, l "Source: '% s'/R/N", BSTR );
Pwszbuffpos + = cbbuf;
Sysfreestring (BSTR );

// Get error parameter information
If (failed (hR = pissceerror-> get_params (& pissceparams) goto exit;

// Obtain the number of Error Parameters
If (failed (hR = pissceparams-> get_count (& lparamcount) goto exit;

// Display each parameter
For (lparamindex = 0; lparamindex <lparamcount; lparamindex ++)
{

// Get the error parameter object
Var. Vt = vt_i4;
Var. lval = lparamindex;
If (failed (hR = pissceparams-> get_item (VAR, & pissceparam) goto exit;

// Get and display the error parameter value
If (failed (hR = pissceparam-> get_param (& varparam) goto exit;
If (vt_i4 = varparam. vt | vt_ui4 = varparam. Vt)
{
Cbbuf = swprintf (pwszbuffpos, l "P % d: % d/R/N", lparamindex,
(Long) varparam. lval );
}
Else if (vt_i2 = varparam. vt | vt_ui2 = varparam. Vt)
{
Cbbuf = swprintf (pwszbuffpos, l "P % d: % d/R/N", lparamindex,
(Long) varparam. ival );
}
Else if (vt_bstr = varparam. Vt)
{
Cbbuf = swprintf (pwszbuffpos, l "P % d: '% s'/R/N", lparamindex,
Varparam. bstrval );
}
Pwszbuffpos + = cbbuf;

// Clear parameter variables
Variantclear (& varparam );

// Release the parameter object
Pissceparam-> release ();
Pissceparam = NULL;
}
Cbbuf = swprintf (pwszbuffpos, l "/R/N ");
Pwszbuffpos + = cbbuf;

}
 
// Use the pop-up box to display the error message
: MessageBox (null, wszbuff, l "error", mb_ OK );
Fsuccess = true;
 
Exit:
// Release the error parameter object
If (pissceparam)
{
Pissceparam-> release ();
Pissceparam = NULL;
}
 
// Release the error parameter set object
If (pissceparams)
{
Pissceparams-> release ();
Pissceparams = NULL;
}
 
// Release the error object
If (pissceerror)
{
Pissceerror-> release ();
Pissceerror = NULL;
}
 
// If an error message fails to be obtained
If (! Fsuccess)
{
: MessageBox (null, l "failed to get error message! ", L" Display error ", mb_ OK );
}
Return;
}
Related Article

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.