Home HomeVB.NET Module 4 Object Oriented Design for Visual Basic .NETDesign Patterns and Object Oriented Programming in Visual Basic 6 and VB.NET (VBL)(ebook computers) Visual C .NET DeveloperÂ’s Guidevisual basicTerry Pratchett 21 WiedzmikolSAMS Teach Yourself PHP4 in 24 HoursTeach Yourself DirectX 7 in 24 Hours (2)Teach yourself linux in 24 hours'Prawo Turystyczne' R.WalczakMartin George R.R Gra o tron (SCAN dal 908)
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • mostlysunny.pev.pl
  •  

    [ Pobierz całość w formacie PDF ]
    .None of these functions take any parameters because they performthe functions that you would expect them to perform. 021 31240-9 CH15 4/27/00 12:55 PM Page 353Updating and Adding Database Records Through ADO 353Along with these functions, the Recordset object also has two properties, BOF and EOF(which you should normally rename to prevent a collision with the default definition of15EOF), which can be checked to determine if the current record in the set is beyond eitherend of the set of records.Accessing Field ValuesWhen you need to begin accessing the data values in each of the fields is where workingwith ADO in Visual C++ begins to get interesting.Because ADO is intended to be easyto use in Microsoft s scripting languages, VBScript and JScript, which only havevariant data types, all data elements that you ll retrieve from fields in the ADORecordset are variant values.They have to be converted into the data types that youneed them to be.There are two ways of doing this.The first way is the straight-forwardway of retrieving the values into a variant and then converting them, as in the followingcode:_variant_t vFirstName;CString strFirstName;vFirstName = pRs->GetCollect(_variant_t( FirstName ));vFirstName.ChangeType(VT_BSTR);strFirstName = vFirstName.bstrVal;The not-so-straight-forward way to do this is actually the better way, and in the long run,is a lot easier to work with.Microsoft has created a series of macros that perform theconversion for you and that maintain a set of variables of the records in the set.To dothis, you ll define a new class to use as the interface for your record set.This class willbe a descendent of the CADORecordBinding class, which is defined in the icrsint.hheader file, which you included just after the #import directive.This class will not haveany constructor or destructor but will have a series of macros, along with a number ofvariables.Each field in the set of records has two variables, an unsigned long, which isused to maintain the status of the variable, and the field variable itself.These variablesmust be regular C variables, and they cannot be C++ classes such as CString.A simpleexample of this class declaration is the following:class CCustomRs :public CADORecordBinding{BEGIN_ADO_BINDING(CCustomRs)ADO_FIXED_LENGTH_ENTRY(1, adInteger, m_lAddressID, lAddressIDStatus,¥'FALSE)ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_szFirstName,¥'sizeof(m_szFirstName), lFirstNameStatus, TRUE)ADO_FIXED_LENGTH_ENTRY(3, adDate, m_dtBirthdate, lBirthdateStatus,¥'TRUE)ADO_FIXED_LENGTH_ENTRY(4, adBoolean, m_bSendCard, lSendCardStatus,¥'TRUE) 021 31240-9 CH15 4/27/00 12:55 PM Page 354354 Day 15END_ADO_BINDING()public:LONG m_lAddressID;ULONG lAddressIDStatus;CHAR m_szFirstName[51];ULONG lFirstNameStatus;DATE m_dtBirthdate;ULONG lBirthdateStatus;VARIANT_BOOL m_bSendCard;ULONG lSendCardStatus;};Once you define this record layout class to match the record layout that will be returnedby your database query, you can declare a variable of this class for use in your applica-tion, as follows:CCustomRs m_rsRecSet;Next, you need to create a pointer to an IADORecordBinding interface, as follows:IADORecordBinding *picRs = NULL;This is a pointer to a COM interface that is part of the ADO Recordset object.Once youretrieve the set of records, you need to retrieve the pointer to the IADORecordBindinginterface and bind the custom record set class to the Recordset object, as in the follow-ing code:if (FAILED(pRs->QueryInterface(__uuidof(IADORecordBinding), (LPVOID¥'*)&picRs)))_com_issue_error(E_NOINTERFACE);picRs->BindToRecordset(&m_rsRecSet);Now, as you navigate the records in the set, you just need to access the member variablesof your custom record class to retrieve the current value for each field.The BEGIN_ADO_BINDING and END_ADO_BINDING MacrosThe key to the second method of accessing the data values in the record set is in themacros that are used in defining the record class.The set of macros start with theBEGIN_ADO_BINDING macro, which takes the class name as its only parameter.This macrosets up the structure definition that is created with the rest of the macros that follow.The set of macros is closed by the END_ADO_BINDING macro.This macro doesn t take anyparameters, and it wraps up the definition of the record binding structure that is createdin the class.It is in the rest of the macros, which are used between these two, where thereal work is done. 021 31240-9 CH15 4/27/00 12:55 PM Page 355Updating and Adding Database Records Through ADO 355The ADO_FIXED_LENGTH_ENTRY MacrosThe ADO_FIXED_LENGTH_ENTRY macro is used for any database fields that are fixed in15size.It can be used with a date or boolean field, or even a text field that is a fixed size,with no option for any variation in the database.There are two versions of this macro;you add a 2 to the end of the name of the second version (ADO_FIXED_LENGTH_ENTRY2).Both versions require the same first three and last parameters.The first version requiresan additional parameter that is not required in the second version.The first parameter isthe ordinal number of the field in the record set.This is the position in the field order asreturned by the SQL query that is run to populate the record set.The second parameter isthe data type of the field; the available data types are defined in the header file createdby the #import directive.The third parameter is the variable into which the data value isto be copied.For the first version of the macro, the fourth parameter is the variable forthe field status (the unsigned long that you defined with the variable for the actualvalue).The last variable is a boolean that specifies whether this field can be modified.The ADO_NUMERIC_ENTRY MacrosYou use the ADO_NUMERIC_ENTRY macros with numeric fields only [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • syriusz777.pev.pl
  •