| 发表于:2007-08-16 10:55:5115楼 得分:0 |
msdn的示例代码如下,我原来用的是int,其他的都和示例基本一样,可就是不成,最后发现是类型不同,呵呵,人家用的是short,俺用的是int或者long,哈哈 void itemx(void) { hresult hr = s_ok; // define ado object pointers. // initialize pointers on define. // these are in the adodb:: namespace. _connectionptr pconnection = null; _recordsetptr prst = null; _commandptr pcmd = null; _parameterptr pprm = null; fieldptr pfld = null; // other variables. _bstr_t strcnn( "provider=sqloledb;data source=myserver; " "initial catalog=pubs;user id=sa;password=; "); _variant_t column[9]; _variant_t vindex; try { // open connection. testhr(pconnection.createinstance(__uuidof(connection))); testhr(prst.createinstance(__uuidof(recordset))); testhr(pcmd.createinstance(__uuidof(command))); //set the array with the authors table field (column) names column[0] = "au_id "; column[1] = "au_lname "; column[2] = "au_fname "; column[3] = "phone "; column[4] = "address "; column[5] = "city "; column[6] = "state "; column[7] = "zip "; column[8] = "contract "; _bstr_t strtext( "select * from authors where state = ? "); pcmd-> commandtext = strtext; pprm = pcmd-> createparameter( "itemxparm ", adchar, adparaminput, 2, "ca "); pcmd-> parameters-> append(pprm); pconnection-> open(strcnn, " ", " ", adconnectunspecified); pcmd-> activeconnection = pconnection; // connection and commandtype are omitted because // a command object is provided. _variant_t conn; conn.vt = vt_error; conn.scode = disp_e_paramnotfound; prst-> open((_variant_t((idispatch *) pcmd)),conn,adopenstatic, adlockreadonly, -1); printf( "the parameters collection accessed by index...\n "); vindex = (short) 0; pprm = pcmd-> parameters-> getitem(&vindex); printf( "parameter name = '%s ', value = '%s '\n ", (lpcstr)pprm-> name, (lpstr)(_bstr_t)pprm-> value); printf( "\nthe parameters collection accessed by name...\n "); pprm = pcmd-> parameters-> item[ "itemxparm "]; printf( "parameter name = '%s ', value = '%s '\n ", (lpcstr)pprm-> name, (lpstr)(_bstr_t)pprm-> value); printf( "\nthe fields collection accessed by index...\n "); prst-> movefirst(); long limit = 0; limit = ((prst-> fields-> count) - 1); int intlinecnt = 8; for (short iindex = 0; iindex <= limit; iindex++) { vindex = iindex; intlinecnt++; if (intlinecnt%15 == 0) { printf( "\npress any key to continue...\n "); getch(); } pfld = prst-> fields-> getitem(&vindex); printf( "field %d : name = '%s ', ", iindex, (lpcstr)pfld-> getname()); _variant_t fldval = pfld-> getvalue(); // because value is the default property of a // property object,the use of the actual keyword // here is optional. switch(fldval.vt) { case (vt_bool): if(fldval.boolval) { printf( "value = 'true ' "); } else { printf( "value = 'false ' "); } printf( "\n "); break; case (vt_bstr): printf( "value = '%s ' ", (lpcstr)(_bstr_t)fldval.bstrval); printf( "\n "); break; case (vt_i4): printf( "value = '%s ' ",(lpcstr)fldval.ival); printf( "\n "); break; case (vt_empty): printf( "value = '%s ' ",(lpcstr)fldval.lval); printf( "\n "); break; default: break; } } printf( "\nthe fields collection accessed by name...\n "); prst-> movefirst(); for (iindex = 0; iindex <= 8; iindex++) { intlinecnt++; if (intlinecnt%15 == 0) { printf( "\npress any key to continue...\n "); getch(); } pfld = prst-> fields-> getitem(column[iindex]); printf( "field name = '%s ', ",(lpcstr)pfld-> getname()); _variant_t fldval = pfld-> getvalue(); // because value is the default property of a // property object,the use of the actual keyword // here is optional. switch(fldval.vt) { case (vt_bool): if(fldval.boolval) { printf( "value = 'true ' "); } else { printf( "value = 'false ' "); } printf( "\n "); break; case (vt_bstr): printf( "value = '%s ' ", (lpcstr)(_bstr_t)fldval.bstrval); printf( "\n "); break; case (vt_i4): printf( "value = '%s ' ",(lpcstr)fldval.ival); printf( "\n "); break; case (vt_empty): printf( "value = '%s ' ",(lpcstr)fldval.lval); printf( "\n "); break; default: break; } } // clean up objects before exit. prst-> close(); pconnection-> close(); } catch(_com_error &e) { // notify the user of errors if any. // pass a connection pointer accessed from the recordset. printprovidererror(pconnection); printcomerror(e); } } | | |
|