You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NumericScale and Precision Properties Example (VC++) | Microsoft Docs
ms.prod
sql
ms.prod_service
connectivity
ms.technology
connectivity
ms.custom
ms.date
01/20/2017
ms.reviewer
ms.topic
conceptual
dev_langs
C++
helpviewer_keywords
NumericScale property [ADO], VC++ example
Precision property [ADO], VC++ example
ms.assetid
55d91ba8-4d80-4df6-af8e-060a19ddc138
author
MightyPen
ms.author
genemi
NumericScale and Precision Properties Example (VC++)
This example uses the NumericScale and Precision properties to display the numeric scale and precision of fields in the Discounts table of the Pubs database.
// BeginNumericScaleCpp.cpp // compile with: /EHsc
#import"msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include<ole2.h>
#include<stdio.h>
#include<conio.h>// Function declarations inlinevoidTESTHR(HRESULT x) { ifFAILED(x) _com_issue_error(x); };
voidNumericScaleX();
voidPrintProviderError(_ConnectionPtr pConnection);
voidPrintComError(_com_error &e);
intmain() {
if ( FAILED(::CoInitialize(NULL)) )
return -1;
NumericScaleX();
::CoUninitialize();
}
voidNumericScaleX() {
// Define ADO object pointers. Initialize pointers on define. // These are in the ADODB:: namespace
_RecordsetPtr pRstDiscounts = NULL;
FieldsPtr fldTemp = NULL;
// Define Other Variables _variant_t Index;
Index.vt = VT_I2;
_bstr_tstrCnn("Provider='sqloledb'; Data Source='My_Data_Source'; Initial Catalog='pubs'; Integrated Security='SSPI';");
try {
// Open recordset. TESTHR(pRstDiscounts.CreateInstance(__uuidof(Recordset)));
pRstDiscounts->Open("discounts", strCnn, adOpenForwardOnly, adLockReadOnly, adCmdTable);
// Display numeric scale and precision of numeric and small integer fields.
fldTemp = pRstDiscounts->GetFields();
for (shortint intLoop = 0 ; intLoop < (int)fldTemp->GetCount() ; intLoop++ ) {
Index.iVal = intLoop;
if ( (fldTemp->GetItem(Index)->Type == adNumeric) ||
(fldTemp->GetItem(Index)->Type == adSmallInt) ) {
printf("Field: %s\n" , (LPCSTR)fldTemp->GetItem(Index)->GetName());
printf("Numeric scale: %d\n", fldTemp->GetItem(Index)->GetNumericScale());
printf("Precision: %d\n", fldTemp->GetItem(Index)->GetPrecision());
}
}
}
catch(_com_error &e) {
// Display errors, if any. Pass a connection pointer accessed from the Recordset. _variant_t vtConnect = pRstDiscounts->GetActiveConnection();
// GetActiveConnection returns connect string if connection // is not open, else returns Connection object. switch(vtConnect.vt) {
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
// Clean up objects before exit. if (pRstDiscounts)
if (pRstDiscounts->State == adStateOpen)
pRstDiscounts->Close();
}
voidPrintProviderError(_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("\t Error number: %x\t%s", pErr->Number, pErr->Description);
}
}
}
voidPrintComError(_com_error &e) {
_bstr_tbstrSource(e.Source());
_bstr_tbstrDescription(e.Description());
// Print COM errors. printf("Error\n");
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);
}