''''///// 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. 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.)
Else
strNoteText = Rs.Fields("NoteText")
End If
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. The remaining three statements close the database objects if they are open (errors can occur before they are used, thus the conditionality).
''''///// 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
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
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




