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

Creating a Server Component with VB - Redesigned - Part 2

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
Added to the database template code are two string variables -- sql and NoteText. The former is used to hold a valid SQL statement that selects the NoteText from the NoteTable. The value that was sent as the first method parameter (lngNoteID) will be used in the SQL statement to select which note text the user wants to view. The value of the "sql" string variable could be assigned directly to objCmd.ComandText, but using a string variable can add flexibility, especially if you later decide to make the SQL statement a method parameter value. The latter string variable is used to hold the note text returned from the database.

Once the "sql" variable is assigned an SQL select statement using the sent lngNoteID, we indicate that the command we''''re sending the Command object is an SQL text statement with the adCmdText VB constant (objCmd.CommandType = adCmdText). We then set the Command object to the Connection object (Set objCmd.ActiveConnection = objConn) and open the recordset (Rs.Open objCmd) with the SQL statement set within the Command object.


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

Now that the recordset is open, the NoteText field of the NoteTable is used within the Rs.Field("NoteText") method to retrieve the NoteText field value from our database record and assign it to our strNoteText variable.

    ''''///// Get NextText from database
    strNoteText = Rs.Fields("NoteText")

The remaining added code closes up the recordset (Rs.Close), sets the database objects to "Nothing" (Set Rs = Nothing, Set objCmd = Nothing, Set objConn = Nothing), and sends the note text value back to the calling code (ShowNotes = strNoteText).

    ''''///// Close this Recordset
    Rs.Close

    ''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
...
    Set Rs = Nothing
    Set objCmd = Nothing
    Set objConn = Nothing

    ''''///// SEND BACK NOTE TEXT FROM DATABASE
    ShowNotes = strNoteText
 
Handling Database Errors

We''''re ready to combine the error code we explored previously with the database code we just developed. Again, this code example will demonstrate how to use transactional processes by "rolling back" all SQL statements when an error occurs in any one SQL statement. Of course, we''''re only using a single select SQL statement, but the process is the same for including multiple SQL statements that necessitate an "all or nothing" database commitment.

We''''ll also use the error processing to send a customized error message back to the calling code if our SQL statement happens to be invalid because an incorrect NoteID was sent as a method parameter. This is important because we really don''''t have any control over what NoteID value might be sent to our method code and we want to let the calling code deal with an erroneous NoteID in its own way - since it''''s the code that sent it.


Adding Error Handling to Database Code   
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
    

    ''''--> 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 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
    Err.Raise Err.Number, Err.Source, Err.Description

End Function

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