ADO can be effectively used in many different ways to accomplish the same results when accessing database information. The code below is a template that will connect to the example database, using NotesDSN as an ODBC data source name, and then disconnect from the database without any fanfare.
Basic Database Code Template The database code first declares variables set to the Recordset, Command, and Connection objects of the ADODB class we referenced with VB previously.
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 Rs As New ADODB.Recordset
Dim objCmd As New ADODB.Command
Dim objConn As New ADODB.Connection
''''///// OPEN DATABASE ///////////////////////////////////////////
objConn.Open strDbConnectionString
objConn.BeginTrans
''''--> DO DATABASE PROCESSING HERE (SELECT/UPDATE/DELETE/INSERT)
''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
objConn.CommitTrans
objConn.Close
End Function
Then the code opens a database connection using the data source name variable strDbConnectionString. Setting the strDbConnectionString method parameter from a calling ASP file can change this connection string variable, but it defaults to NotesDSN, which we''''ll use as our ODBC data source name for our example code.
Dim Rs As New ADODB.Recordset
Dim objCmd As New ADODB.Command
Dim objConn As New ADODB.Connection
The next line of code isn''''t really necessary for our example program, but I included it to exemplify how to code for transactions. (See the article title "How to Utilize Database Transactions Within a VB Component From an asp File" at http://www.dougdean.com for more information on database transactions.)
objConn.Open strDbConnectionString
By using transactions, a method can treat a series of database processes (SQL statements) as a complete unit. If only one SQL statement in a series of SQL statements fails, then all the SQL statements within the same transaction are rolled back to the state the database was in before the transaction began. So if you''''re moving a record from one table to another table by adding it to the second table and then deleting it from the first table, you can be assured that one SQL statement won''''t take place without the other when they are within a transaction.
objConn.BeginTrans
As I mentioned, our database sample code will not actually need this type of transaction commitment since all we''''re doing is reading from the tables. But if you do any editing or deleting, you''''ll find using the transaction code demonstrated here to be necessary at some point.
Once the database connection is opened, and we have indicated that we want a transaction to begin, we do some database processing (adding, deleting, or changing fields via SQL statements) and then finish up our database effort by telling ADO that we want all of our SQL statements processed as a single unit (objConn.CommitTrans) before closing the database connection (objConn.Close). To demonstrate how to retrieve one of the database records, we can add code to the template described above. This method code example will access the database and then send the note text of a record back to the ASP file code that called it. This method code assumes that a valid NoteID will be used as the first method parameter.
Adding Database Select Code
文章整理:西部数码--专业提供域名注册、虚拟主机服务
Getting Note Text from the Database
''''--> DO DATABASE PROCESSING HERE (SELECT/UPDATE/DELETE/INSERT)
''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
objConn.CommitTrans
objConn.Close
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
''''///// 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
''''///// Get NextText from database
strNoteText = Rs.Fields("NoteText")
''''///// Close this Recordset
Rs.Close
''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
objConn.CommitTrans
objConn.Close
Set Rs = Nothing
Set objCmd = Nothing
Set objConn = Nothing
''''///// SEND BACK NOTE TEXT FROM DATABASE
ShowNotes = strNoteText
End Function
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




