; _RecordsetPtr pRs("ADODB.Recordset"); _ConnectionPtr pCn("ADODB.Connection"); _variant_t vtTableName("authors"), vtCriteria; long ix[1]; SAFEARRAY *pSa = NULL; pCn->Open("DSN=pubs;User ID=sa;pwd=;Provider=MSDASQL;", "", "", adConnectUnspecified); // Note 2, Note 3 pSa = SafeArrayCreateVector(VT_VARIANT, 1, 4); if (!pSa) _com_issue_error(E_OUTOFMEMORY); // 为第三个元素赋值TABLE_NAME(索引值2). ix[0] = 2; TESTHR(SafeArrayPutElement(pSa, ix, &vtTableName)); // 由于Variant没有SafeArray的构造函数,所以手工设置Variant的数据类型和值。 vtCriteria.vt = VT_ARRAY | VT_VARIANT; vtCriteria.parray = pSa; pRs = pCn->OpenSchema(adSchemaColumns, vtCriteria, vtMissing); long limit = pRs->GetFields()->Count; for (long x = 0; x < limit; x++) printf("%d: %s\n", x+1, ((char*) pRs->GetFields()->Item[x]->Name)); // Note 4 pRs->Close(); pCn->Close(); } catch (_com_error &e) { printf("Error:\n"); printf("Code = %08lx\n", e.Error()); printf("Code meaning = %s\n", (char*) e.ErrorMessage()); printf("Source = %s\n", (char*) e.Source()); printf("Description = %s\n", (char*) e.Description()); } CoUninitialize(); } 使用属性的Get/Put/PutRef 在VB中,属性的名称并未被检验,无论它是被读取、被赋值,或者赋予一个引用。 Public Sub GetPutPutRef Dim rs As New ADODB.Recordset Dim cn As New ADODB.Connection Dim sz as Integer cn.Open "Provider=sqloledb;Data Source=yourserver;" & _ "Initial Catalog=pubs;User Id=sa;Password=;" rs.PageSize = 10 sz = rs.PageSize rs.ActiveConnection = cn rs.Open "authors",,adOpenStatic '''' ... rs.Close cn.Close End Sub 以下是VC++关于Get/Put/PutRefProperty的演示 1.这个例子演示了省略字符串参数的两种形式:一种是采用常量strMissing,另一种则是由编译器自动生成一个临时的存在于Open方法使用期间的_bstr_t。 2.因为操作数已经是(IDispatch *)的指针,所以没有必要将rs->PutRefActiveConnection(cn)的操作数再进行类型转换。 #import "c:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") #include <stdio.h> void main(void) { CoInitialize(NULL); try { _ConnectionPtr cn("ADODB.Connection"); _RecordsetPtr rs("ADODB.Recordset"); _bstr_t strMissing(L""); long oldPgSz = 0, newPgSz = 5; // Note 1 cn->Open("Provider=sqloledb;Data Source=a-tima10;" "Initial Catalog=pubs;User Id=sa;Password=;", strMissing, "", adConnectUnspecified); oldPgSz = rs->GetPageSize(); // -or- oldPgSz = rs->PageSize; rs->PutPageSize(newPgSz); // -or- rs->PageSize = newPgSz; // Note 2 rs->PutRefActiveConnection( cn ); rs->Open("authors", vtMissing, adOpenStatic, adLockReadOnly, adCmdTable); printf("Original pagesize = %d, new pagesize = %d\n", oldPgSz, rs->GetPageSize()); rs->Close(); cn->Close(); } catch (_com_error &e) { printf("Description = %s\n", (char*) e.Description()); 上一页 [1] [2] [3] [4] [5] [6] 下一页 没有相关教程
|