attributesx.cpp

来自「visual c++ 很全的ado的sample」· C++ 代码 · 共 184 行

CPP
184
字号
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
    no_namespace rename("EOF", "EndOfFile")

#include <ole2.h>
#include <stdio.h>
#include<conio.h>

// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void AttributesX();
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

///////////////////////////////////////////////////////////
//                                                       //
//      Main Function                                    //
//                                                       //
///////////////////////////////////////////////////////////

void main()
{
	if(FAILED(::CoInitialize(NULL))) 
		return;

	AttributesX();

	//Wait here for user to see the output..
	printf("\nPress any key to continue...");
	getch();

	::CoUninitialize();
}


///////////////////////////////////////////////////////////
//                                                       //
//      AttributesX Function                             //
//                                                       //
///////////////////////////////////////////////////////////

void AttributesX()
{
	// Define ADO object pointers.
    // Initialize pointers on define.
    // These are in the ADODB::  namespace
	 _RecordsetPtr	pRstEmployee  = NULL;
	 _ConnectionPtr pConnection = NULL;
	 FieldsPtr  fldLoop = NULL;    
	 PropertiesPtr proLoop = NULL;

	 //Define Other Variables
	HRESULT  hr = S_OK;
  	_variant_t Index;
	Index.vt = VT_I2;
	int j=0;      
    //Open a recordset using a Client Cursor
	//For the Employee Table
   	_bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;"
	"Initial Catalog=pubs;User Id=sa;Password=;");

  	try
	{
		// open connection and record set
		TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
		pConnection->Open(strCnn,"","",NULL);
		
		TESTHR(pRstEmployee.CreateInstance(__uuidof(Recordset)));
		pRstEmployee->Open("Employee", _variant_t((IDispatch *)pConnection,true), adOpenForwardOnly,
			adLockReadOnly, adCmdTable);
	
		// Display the attributes of Connection.
		printf("Connection attributes: %d \n", pConnection->Attributes);
   
		// Display the attribute of the employee table's 
		//fields
		printf("\nFields attributes:\n");
		fldLoop = pRstEmployee->GetFields();
	
		for (int i = 0; i < (int)fldLoop->GetCount(); i++)
		{
			Index.iVal=i;
			printf ("   %s = %d \n",(LPSTR)fldLoop->GetItem(Index)->GetName(),
				(int)fldLoop->GetItem(Index)->GetAttributes());
		}
		
		// Display Fields of the Employee table which are NULLBALE
		printf("\nNULLABLE Fields :");
		
		for (int i1 = 0; i1 < (int)fldLoop->GetCount(); i1++)
		{
			Index.iVal = i1;
			
			if (fldLoop->GetItem(Index)->GetAttributes()  & adFldIsNullable)
				{
					printf ("%s  \n", (LPSTR)fldLoop->GetItem(Index)->GetName());	
				}
		}

	    // Display the attributes of the Employee tables's 
	    // properties
	    printf("\nProperty attributes:\n");
	    proLoop = pRstEmployee->GetProperties();
	  
	    for (int i2 = 0; i2 < (int)proLoop->GetCount(); i2++)
		{
			j= j+1;
			Index.iVal=i2;
			printf (" %s = %d \n", (LPSTR)(_bstr_t)proLoop->GetItem(Index)->GetName()
			,(int)proLoop->GetItem(Index)->GetAttributes()); 
		
			if (((j % 23) == 0) || ( i2==6))
			{
				printf("\nPress any key to continue...");
				getch();
	
				//Clear the screen for the next display   
				system("cls"); 
				j=0;
			}
	
		}
	  // Clean up objects before exit.  
	  pRstEmployee->Close();
	  pConnection->Close();
	}
	
	catch(_com_error &e)
	{
		// Notify the user of errors if any.
		
		PrintProviderError(pConnection);
		PrintComError(e);
	}

}


///////////////////////////////////////////////////////////
//                                                       //
//      PrintProviderError Function                      //
//                                                       //
///////////////////////////////////////////////////////////

void PrintProviderError(_ConnectionPtr pConnection)
{
	// Print Provider Errors from Connection object.
	
	// pErr is a record object in the Connection's Error collection.
	ErrorPtr    pErr  = NULL;      
	long      nCount  = 0;    
	long       i    = 0;

	if( (pConnection->Errors->Count) > 0)
	{
		nCount = pConnection->Errors->Count;

		// Collection ranges from 0 to nCount -1.
		for(i = 0; i < nCount; i++)    
		{
			pErr = pConnection->Errors->GetItem(i);
			printf("\t Error number: %x\t%s", (LPCSTR) pErr->Number, (LPCSTR) pErr->Description);
		}
	}
}

///////////////////////////////////////////////////////////
//                                                       //
//      PrintComError Function                           //
//                                                       //
///////////////////////////////////////////////////////////

VOID PrintComError(_com_error &e)
{
   _bstr_t bstrSource(e.Source());
   _bstr_t bstrDescription(e.Description());
	
	// Print Com errors.  
   printf("\nError\n");
   printf("Code = %08lx\n", e.Error());
   printf("Code meaning = %s\n", e.ErrorMessage());
   printf("Source = %s\n", (LPCSTR) bstrSource);
   printf("Description = %s\n", (LPCSTR) bstrDescription);  
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?