使用属性的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++(www.cppentry.com)关于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()); } ::CoUninitialize(); } |
使用GetItem(x)和Item[x]
下面是VB中关于Item()的标准与交互语法的演示。
Public Sub GetItemItem Dim rs As New ADODB.Recordset Dim name as String rs = rs.Open "authors", "DSN=pubs;", adOpenDynamic, _ adLockBatchOptimistic, adTable name = rs(0) ' -or- name = rs.Fields.Item(0) rs(0) = "Test" rs.UpdateBatch ' Restore name rs(0) = name rs.UpdateBatch rs.Close End Sub |
以下则是VC++(www.cppentry.com)关于Item的演示
当访问collection中的Item时,索引值2必须被转换为long类型以确保正确的构造函数被调用。
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") #include <stdio.h>
void main(void) { CoInitialize(NULL); try { _RecordsetPtr rs("ADODB.Recordset"); _variant_t vtFirstName;
rs->Open("authors", "Provider=sqloledb;Data Source=a-tima10;" "Initial Catalog=pubs;User Id=sa;Password=;", adOpenStatic, adLockOptimistic, adCmdTable); rs->MoveFirst();
// Note 1.取得一个字段的名称 vtFirstName = rs->Fields->GetItem((long)2)->GetValue(); // -or- vtFirstName = rs->Fields->Item[(long)2]->Value;
printf( "First name = '%s'\n", (char*) ((_bstr_t) vtFirstName));
rs->Fields->GetItem((long)2)->Value = L"TEST"; rs->Update(vtMissing, vtMissing);
// 恢复原名称 rs->Fields->GetItem((long)2)->PutValue(vtFirstName); // -or- rs->Fields->GetItem((long)2)->Value = vtFirstName; rs->Update(vtMissing, vtMissing); rs->Close(); } catch (_com_error &e) { printf("Description = '%s'\n", (char*) e.Description()); } ::CoUninitialize(); }
|
|