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

Creating a Server Component with VB - Redesigned - Part 2

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
To verify whether a record was correctly returned from the database, we''''ll check the recordset end of file (EOF) and beginning of file (BOF) values. If both EOF and BOF are true, then we know that no record has been returned and something went astray. The most likely explanation is that the ASP code sent a NoteID as a method parameter value that doesn''''t exist in our database. Even though we know that the NoteID''''s links with "ID" query string values within the titles that the VB method sends back to the ASP file are valid, this method needs to handle links from any URL Internet connection that might attach an ID query string value.

    ''''///// Check if any data was returned
    If Rs.EOF = True And Rs.BOF = True Then 

If no record was returned, we''''ll invoke the same type of error-messaging code covered previously. Using a custom error-number value that typifies this particular category of error will let the calling code check for a situation that results in nonexistent NoteID records. Also, clearly stating the location of the error, along with a succinct description string, will help with debugging as your code grows large.

        ''''///// No data returned, set error information, raise error
        Err.Number = 40001
        Err.Source = "NoteProject::NoteClass::ShowNotes"
        Err.Description = "00000002<BR>Possible invalid lngNoteID entered as method value"
        Err.Raise Err.Number, Err.Source, Err.Description 

Here "40001" is used to indicate an invalid NoteID value. The project, class, and method names are used as the source value. The description includes a unique numbering scheme (I use a unique hexadecimal number for each error I trap) and a short description of the error, which includes an HTML <BR> since an ASP file will be used to call this method.

After assigning our error message values, we raise our error and send it off to the ErrorCode section at the very bottom of the method code, skipping all other method code. Of course, if a record is successfully returned, the Else section of our If/Then code will retrieve the record''''s NoteText field value and omit any of our error code.


    Else
        strNoteText = Rs.Fields("NoteText")
    End If

You may want to test this example code with both valid and invalid NoteID values to see the error processing in action. (Compile the code to test this rather than just running the code since VB will process the error differently in each situation.)

Down at the bottom of our method we have our error section that processes any errors that occur in our method, as well as the ones we raise ourselves. Since we originally opened a database transaction (and getting to this error code section assumes that we have skipped over the section of code that commits our database transaction), we need to let ADO know that we want any previous SQL statements undone. We do this with the objConn.RollbackTrans statement that rolls back any database work from the point in our code where we placed our objConn.BeginTrans statement.


    ''''///// ROLLBACK, CLOSE DB REFERENCES, AND SEND BACK RAISED ERROR
    objConn.RollbackTrans
    If IsObject(Rs) Then Set Rs = Nothing
    If IsObject(objCmd) Then Set objCmd = Nothing
    If IsObject(objConn) Then Set objConn = Nothing

The remaining three statements close the database objects if they are open (errors can occur before they are used, thus the conditionality).

Complete Code for Retrieving a Note''''s Text Here is the complete code for retrieving a note''''s text from the database. It''''s put together here so you can get a good overview of what has been discussed up to this point. We''''ll be adding more code to our method. At this point it might be advantageous to make sure you comprehend this code before going on to the next code section, which will demonstrate an alternative way to handle retrieved database values.

Here''''s what we covered so far:

Complete Code for Select Database Process


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

    ''''///// ERROR CODE //////////////////////////////////////////////
    On Error GoTo ErrorCode

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

    ''''///// DB Value Storage variables
    Dim strNoteText As String

    ''''///// OPEN DATABASE ///////////////////////////////////////////
    objConn.Open strDbConnectionString
    objConn.BeginTrans

    ''''///// GET SELECTED NOTE TEXT FROM DATABASE ////////////////////
    sql = "SELECT NoteText FROM NoteTable WHERE NoteID = " & lngNoteID
    objCmd.CommandText = sql
    objCmd.CommandType = adCmdText
    Set objCmd.ActiveConnection = objConn
    Rs.Open objCmd

    ''''///// Check if any data was returned
    If Rs.EOF = True And Rs.BOF = True Then
        ''''///// No data returned, set error information, raise error
        Err.Number = 40001
        Err.Source = "NoteProject::NoteClass::ShowNotes"
        Err.Description = "00000002<BR>Possible invalid lngNoteID entered as method value"

        Err.Raise Err.Number, Err.Source, Err.Description
    Else
        strNoteText = Rs.Fields("NoteText")
    End If
    
    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


    ''''///// SEND BACK NOTE TEXT FROM DATABASE
    ShowNotes = strNoteText

    ''''///// EXIT FUNCTION IF NO ERROR RAISED ////////////////////////
    Exit Function
    
    ''''///// CODE IF ERROR OCCURS ////////////////////////////////////
ErrorCode:

    ''''///// ROLLBACK, CLOSE DB REFERRENCES, AND SEND BACK RAISED ERROR
    objConn.RollbackTrans
    If IsObject(Rs) Then Set Rs = Nothing
    If IsObject(objCmd) Then Set objCmd = Nothing
    If IsObject(objConn) Then Set objConn = Nothing
    Err.Raise Err.Number, Err.Source, Err.Description

End Function

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