手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

Creating a Server Component with VB - Redesigned - Part 2

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
In the next code section the VB GetRows() method will be used in place of the Rs.Fields() VB method. GetRows() will be used to obtain all of the note title names stored in the example NoteTable database table. We''''ll also be adding code that transforms our raw database values into an HTML-formatted string ready for viewing in a browser.

Getting Data with the GetRows() VB Method

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()


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

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.

    ''''///// 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 

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.

    ''''///// Populate a Variant array via the GetRows() method
    vRecordArray = Rs.GetRows

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!