The VB GetRows() method provides a convent and fast way to store rows of database record fields. It can seem a bit tricky to access the values stored in this special type of array, but getting the values from the database into the array couldn''''t be easier. In this code section I''''ve omitted the database and error code previous covered so you can see GetRows() without other distracting code.
Here we''''ll need to get the NoteTitle and NoteID for each of our five example records. We could use the Rs.Fields() method within a loop to store the data from the two fields in a couple of arrays, or even compose the HTML text on-the-fly as the code loops though the open recordset. But the GetRows() method makes for a cleaner, simpler, and optimized way to work with rows of record fields.
The generic code for opening and closing a database used here is basically the same as described before, sans the code specific to the note text and error processes. We''''ll combine the previous code for retrieving the note''''s text and title after the GetRows() method is discussed.
Code Using GetRows()
文章整理:西部数码--专业提供域名注册、虚拟主机服务Notice that the "vRecordArray" variable (shown below), which is used to store the records fields returned by the GetRows() method in a multidimensional array, is of type Variant. To make our multidimensioned array data easy to read, two constants (ARRAY_NOTE_ID_INDEX and ARRAY_TITLE_INDEX) will be used to identify the location of the two database fields within the array. Two other variables (lngArrayCount and lngIndexCount) will be used to: 1) hold a value representative of the number of records in the array and; 2) as an indexer used when looping through the array.
Public Function ShowNotes(ByVal lngNoteID As Long, _
Optional ByVal intStyleID As Integer = 0, _
Optional ByVal strURL As String = "NoteExample.asp", _
Optional ByVal strDbConnectionString As String = "NotesDSN") As String
''''///// DB Connectivity Variables
Dim sql As String
Dim Rs As New ADODB.Recordset
Dim objCmd As New ADODB.Command
Dim objConn As New ADODB.Connection
''''///// Variable to hold constructed text
Dim strReturnString As String
''''///// DB Value storage array variable
Dim vRecordArray As Variant
''''///// Title Array Constant and Variable
Const ARRAY_NOTE_ID_INDEX = 0
Const ARRAY_TITLE_INDEX = 1
Dim lngArrayCount As Long
Dim lngIndexCount As Long
''''///// OPEN DATABASE ///////////////////////////////////////////
objConn.Open strDbConnectionString
objConn.BeginTrans
''''///// GET NOTE TITLES FROM DB AND PLACE IN GetRows ////////////
sql = "SELECT NoteID, NoteTitle FROM NoteTable"
objCmd.CommandText = sql
objCmd.CommandType = adCmdText
Set objCmd.ActiveConnection = objConn
Rs.Open objCmd
''''///// Populate a Variant array via the GetRows() method
vRecordArray = Rs.GetRows
''''///// Close this Recordset
Rs.Close
''''--> ANY OTHER DATABASE CODE GOES HERE
''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
objConn.CommitTrans
objConn.Close
Set Rs = Nothing
Set objCmd = Nothing
Set objConn = Nothing
''''--> OTHER METHOD CODE GOES HERE
''''///// Get the number of titles stored in the array
lngArrayCount = UBound(vRecordArray, 2)
''''///// Loop through array
For lngIndexCount = 0 To lngArrayCount
''''///// Get data from the array populated by the GetRows() method
strReturnString = strReturnString & "NoteID = "
strReturnString = strReturnString & vRecordArray(ARRAY_NOTE_ID_INDEX, lngIndexCount)
strReturnString = strReturnString & " NoteText = "
strReturnString = strReturnString & vRecordArray(ARRAY_TITLE_INDEX, lngIndexCount)
strReturnString = strReturnString & "<BR>" & vbCrLf
Next
''''///// SEND BACK CONSTRUCTED STRING /////////////////////////////
ShowNotes = strReturnString
End Function
Here''''s the easiest part. Once the database objects are all in place, and the SQL statement is used to open the Command object, only one line of code is needed to assign the Rs.GetRows() method to the Variant variable.
''''///// DB Value storage array variable
Dim vRecordArray As Variant
''''///// Title Array Constant and Variable
Const ARRAY_NOTE_ID_INDEX = 0
Const ARRAY_TITLE_INDEX = 1
Dim lngArrayCount As Long
Dim lngIndexCount As Long
''''///// Populate a Variant array via the GetRows() method
vRecordArray = Rs.GetRows
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




