CursorType、LockType、EditMode プロパティの例 (vc++)CursorType, LockType, and EditMode Properties Example (VC++)

この例の設定、 CursorTypeLockTypeプロパティを開く前に、レコード セットします。This example demonstrates setting the CursorType and LockType properties before opening a Recordset. 値も表示されます、 EditModeさまざまな条件下でのプロパティ。It also shows the value of the EditMode property under various conditions. EditModeOutput 関数は、この手順を実行する必要があります。The EditModeOutput function is required for this procedure to run.


// CursorType_LockType_EditMode_Property_Example.cpp  
// compile with: /EHsc  
#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")  
#include <ole2.h>  
#include <stdio.h>  
#include <conio.h>  
// Function declaration  
inline void TESTHR(HRESULT x) { if FAILED(x) _com_issue_error(x); };  
void EditModeX();  
void EditModeOutput(char *, int);  
void PrintProviderError(_ConnectionPtr pConnection);  
void PrintComError(_com_error &e);  
int main() {  
   if ( FAILED(::CoInitialize(NULL)) )  
      return -1;  
void EditModeX() {  
   // Define ADO object pointers.  Initialize pointers on define.  
   // These are in the ADODB:: namespace.  
   _RecordsetPtr pRstEmployees = NULL;  
   _ConnectionPtr pConnection = NULL;  
   HRESULT hr = S_OK;  
   _bstr_t strCnn("Provider='sqloledb'; Data Source='My_Data_Source'; Initial Catalog='pubs'; Integrated Security='SSPI';");  
   try {  
      // Open a connection  
      hr = pConnection->Open(strCnn, "", "", adConnectUnspecified);  
      // Open recordset with data from employee table   
      pRstEmployees->CursorLocation = adUseClient;  
      pRstEmployees->CursorType = adOpenStatic;  
      pRstEmployees->LockType = adLockBatchOptimistic;  
      pRstEmployees->Open("employee", _variant_t((IDispatch *) pConnection,true),  
         adOpenStatic, adLockBatchOptimistic, adCmdTable);  
      // Show the EditMode property under different editing states.  
      pRstEmployees->AddNew ();  
      pRstEmployees->Fields->Item["emp_id"]->Value = (_bstr_t)("T-T55555M");  
      pRstEmployees->Fields->Item["fname"]->Value = (_bstr_t)("temp_fname");  
      pRstEmployees->Fields->Item["lname"]->Value = (_bstr_t)("temp_lname");  
      EditModeOutput("After AddNew: ", pRstEmployees->EditMode);  
      EditModeOutput("After Update: ", pRstEmployees->EditMode);  
      pRstEmployees->Fields->Item["fname"]->Value = (_bstr_t)("test");  
      EditModeOutput("After Edit: ", pRstEmployees->EditMode);  
      // Delete new record because this is a demonstration.  
      pConnection->Execute("DELETE FROM employee WHERE emp_id = 'T-T55555M'",   
         NULL, adCmdText);  
   catch(_com_error &e) {  
      // Display errors, if any. Pass connection pointer accessed from the Recordset.  
   // Clean up objects before exit.  
   if (pRstEmployees)  
      if (pRstEmployees->State == adStateOpen)  
   if (pConnection)  
      if (pConnection->State == adStateOpen)  
void EditModeOutput(char *strTemp, int intEditMode) {  
   // Print report based on the value of the EditMode property.  
   printf("\n%s", strTemp);  
   printf("\n  EditMode = ");  
   switch (intEditMode) {  
   case adEditNone :  
   case adEditInProgress :  
   case adEditAdd :  
void PrintProviderError(_ConnectionPtr pConnection) {  
   // Print Provider Errors from Connection object.  
   // pErr is a record object in the Connection's Error collection.  
   ErrorPtr pErr = NULL;  
   if ( (pConnection->Errors->Count) > 0) {  
      long nCount = pConnection->Errors->Count;  
      // Collection ranges from 0 to nCount -1.  
      for ( long i = 0 ; i < nCount ; i++ ) {  
         pErr = pConnection->Errors->GetItem(i);  
         printf("\n\t Error number: %x\t%s\n", pErr->Number, (LPCSTR)pErr->Description);  
void PrintComError(_com_error &e) {  
   _bstr_t bstrSource(e.Source());  
   _bstr_t bstrDescription(e.Description());  
   // Print Com errors.  
   printf("\tCode = %08lx\n", e.Error());  
   printf("\tCode meaning = %s\n", e.ErrorMessage());  
   printf("\tSource = %s\n", (LPCSTR) bstrSource);  
   printf("\tDescription = %s\n", (LPCSTR) bstrDescription);  

AddNew: の後 After AddNew:
EditMode adEditAdd を =EditMode = adEditAdd
後の更新。 After Update:
EditMode adEditNone を =EditMode = adEditNone
[編集]: の後 After Edit:
EditMode adEditInProgress を =EditMode = adEditInProgress

関連項目See Also

CursorType プロパティ (ADO) CursorType Property (ADO)
EditMode プロパティ EditMode Property
LockType プロパティ (ADO) LockType Property (ADO)
Recordset オブジェクト (ADO)Recordset Object (ADO)