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

Creating a Server Component with VB - Redesigned - Part 2

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
The only moderately tricky aspect to using the GetRows() method is indexing the array stored in vRecordArray, where the database record field values can now be found. But our constants will make this easier to do. VRecordArray is a variable name used to store an array. The nature of the array assigned to the vRecordArray Variant variable will depend on the structure of the SQL statement assigned to the database Command object. Since the NoteID and NoteTitle field values are the fields designated in the SQL statement (SELECT NoteID, NoteTitle FROM NoteTable), the vRecordArray will hold a two-dimensional array. The first dimension corresponds to the record values of the NoteID field and the second to the values of the NoteTitle field. Obviously, the order and number of record fields within the SQL statement will correspond to the dimensional indexing of the vRecordArray.

The records returned from the NoteTitle table of our example database, via the SQL statement used here, hold the following values:


SELECT NoteID, NoteTitle FROM NoteTable

NoteID	NoteTitle
1	Intro
2	Style Tables
3	GetRows()
4	Buffering
5	Transactions

The vRecordArray holds a zero-based array with the first array dimension corresponding to the database fields, which are the NoteID and NoteTitle fields in our example code. NoteID will have an index of "0", since it was the first field to be returned in the SQL statement, and NoteTitle will have an index of "1". The second dimension of the array will index the number and order of the NoteID and NoteTitle record fields values that were returned, starting with "0". This all sounds more complex than it is. Here are the corresponding array indexes that match the database field values of the tables displayed above.

sql = "SELECT NoteID, NoteTitle FROM NoteTable"
NoteID			NoteTitle
-------------------------------------------------------------------------
vRecordArray(0, 0) = 1		vRecordArray(1, 0) = "Intro"
vRecordArray(0, 1) = 2		vRecordArray(1, 1) = "Style Tables" 
vRecordArray(0, 2) = 3		vRecordArray(1, 2) = "GetRows()"
vRecordArray(0, 3) = 4		vRecordArray(1, 3) = "Buffering"
vRecordArray(0, 4) = 5		vRecordArray(1, 4) = "Transactions "

Since the ones and zeros can be confusing, we can replace them with the constants that were declared at the start of our code. This makes the code more readable, albeit more verbose.

Const ARRAY_NOTE_ID_INDEX = 0
Const ARRAY_TITLE_INDEX = 1
NoteID					NoteTitle
-------------------------------------------------------------------------
vRecordArray(ARRAY_NOTE_ID_INDEX, 0) = 1	vRecordArray(ARRAY_TITLE_INDEX, 0) = "Intro"
vRecordArray(ARRAY_NOTE_ID_INDEX, 1) = 2	vRecordArray(ARRAY_TITLE_INDEX, 1) = "Style Tables" 
vRecordArray(ARRAY_NOTE_ID_INDEX, 2) = 3	vRecordArray(ARRAY_TITLE_INDEX, 2) = "GetRows()"
vRecordArray(ARRAY_NOTE_ID_INDEX, 3) = 4	vRecordArray(ARRAY_TITLE_INDEX, 3) = "Buffering"
vRecordArray(ARRAY_NOTE_ID_INDEX, 4) = 5	vRecordArray(ARRAY_TITLE_INDEX, 4) = "Transactions "

Since all our database information is tightly stored away in our vRecordArray variable, we''''ll close up our database connection and loop through the array to send back some example demonstration code as a first test of our GetRows() array.

The first thing to do is store the value of the last index of our array in the lngArrayCount variable. This gives us a numerical value of were the array ends (see the code below). Remember, this array is zero based, so the value of lngArrayCount will really be one minus the actual number of items within the array.


     ''''///// Get the number of titles stored in the array
    lngArrayCount = UBound(vRecordArray, 2)

Next, we''''ll loop through our array while storing the string concatenations in the strReturnString variable.

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

The code above will construct a single string, held in strReturnString, with the value displayed below.

NoteID =  1 NoteText =  Intro<BR>
NoteID =  2 NoteText =  Style Tables<BR>
NoteID =  3 NoteText =  GetRows()<BR>
NoteID =  4 NoteText =  Buffering<BR>
NoteID =  5 NoteText =  Transactions<BR>

This VB constant vbCrLf that is assigned to the last line of the code within the loop, and directly after the <BR> string value, will produce a Carriage Return/LineFeed that places a line break at the end of each record line of text for easy HTML source-code reading.

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