This example demonstrates the Prepared property by opening two Command objectsone prepared and one not prepared.
'BeginPreparedVB 'To integrate this code 'replace the data source and initial catalog values 'in the connection string Public Sub PreparedX() Dim Cnxn As ADODB.Connection Dim cmd1 As ADODB.Command Dim cmd2 As ADODB.Command Dim strCnxn As String Dim strCmd As String Dim sngStart As Single Dim sngEnd As Single Dim sngNotPrepared As Single Dim sngPrepared As Single Dim intLoop As Integer ' Open a connection strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=Pubs;User Id=sa;Password=; " Set Cnxn = New ADODB.Connection Cnxn.Open strCnxn ' Create two command objects for the same ' command - one prepared and one not prepared strCmd = "SELECT title, type FROM Titles ORDER BY type" Set cmd1 = New ADODB.Command Set cmd1.ActiveConnection = Cnxn cmd1.CommandText = strCmd Set cmd2 = New ADODB.Command Set cmd2.ActiveConnection = Cnxn cmd2.CommandText = strCmd cmd2.Prepared = True ' Set a timer, then execute the unprepared ' command 20 times sngStart = Timer For intLoop = 1 To 20 cmd1.Execute Next intLoop sngEnd = Timer sngNotPrepared = sngEnd - sngStart ' Reset the timer, then execute the prepared ' command 20 times sngStart = Timer For intLoop = 1 To 20 cmd2.Execute Next intLoop sngEnd = Timer sngPrepared = sngEnd - sngStart ' Display performance results MsgBox "Performance Results:" & vbCr & _ " Not Prepared: " & Format(sngNotPrepared, _ "##0.000") & " seconds" & vbCr & _ " Prepared: " & Format(sngPrepared, _ "##0.000") & " seconds" ' clean up Cnxn.Close Set Cnxn = Nothing End Sub 'EndPreparedVB
Command Object | Prepared Property
© 1998-2001 Microsoft Corporation. All rights reserved.