home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 August / Chip_1999-08_cd.bin / msdn / VSTUDIO / MSDE / msde_vb6.exe / frmMain.frm (.txt) < prev    next >
Visual Basic Form  |  1999-05-13  |  8KB  |  211 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMain 
  3.    Caption         =   "MSDE Sample"
  4.    ClientHeight    =   5775
  5.    ClientLeft      =   1080
  6.    ClientTop       =   1545
  7.    ClientWidth     =   9765
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   5775
  10.    ScaleWidth      =   9765
  11.    Begin VB.Frame Frame1 
  12.       Caption         =   "Connection Type"
  13.       Height          =   975
  14.       Left            =   240
  15.       TabIndex        =   3
  16.       Top             =   120
  17.       Width           =   1575
  18.       Begin VB.OptionButton optConnectionType 
  19.          Caption         =   "DAO"
  20.          Height          =   255
  21.          Index           =   1
  22.          Left            =   120
  23.          TabIndex        =   5
  24.          Top             =   600
  25.          Width           =   735
  26.       End
  27.       Begin VB.OptionButton optConnectionType 
  28.          Caption         =   "ADO"
  29.          Height          =   255
  30.          Index           =   0
  31.          Left            =   120
  32.          TabIndex        =   4
  33.          Top             =   240
  34.          Value           =   -1  'True
  35.          Width           =   735
  36.       End
  37.    End
  38.    Begin VB.CommandButton cmdStoredProc 
  39.       Caption         =   "Stored Procedure"
  40.       Height          =   375
  41.       Left            =   240
  42.       TabIndex        =   2
  43.       Top             =   1200
  44.       Width           =   1575
  45.    End
  46.    Begin VB.TextBox txtResults 
  47.       Height          =   3495
  48.       Left            =   240
  49.       MultiLine       =   -1  'True
  50.       TabIndex        =   0
  51.       Top             =   2040
  52.       Width           =   9255
  53.    End
  54.    Begin VB.Label Label1 
  55.       Caption         =   "Results:"
  56.       Height          =   255
  57.       Left            =   240
  58.       TabIndex        =   1
  59.       Top             =   1800
  60.       Width           =   1335
  61.    End
  62. Attribute VB_Name = "frmMain"
  63. Attribute VB_GlobalNameSpace = False
  64. Attribute VB_Creatable = False
  65. Attribute VB_PredeclaredId = True
  66. Attribute VB_Exposed = False
  67. Option Explicit
  68. Private Sub cmdStoredProc_Click()
  69.     SelectedConnectionType = Not optConnectionType(ConnectionType.ADO).Value
  70.     Dim Counter As Single
  71.     Select Case SelectedConnectionType
  72.         
  73.         Case ConnectionType.ADO
  74.                     
  75.             Dim adoCommand As ADODB.Command
  76.             Dim adoResults As ADODB.Recordset
  77.             Dim adoParm As ADODB.Parameter
  78.             
  79.             Set adoCommand = CreateObject("ADODB.Command")
  80.             Set adoResults = CreateObject("ADODB.Recordset")
  81.             
  82.             With adoCommand
  83.                 ' Indicate that we will be calling a stored procedure.
  84.                 ' You can also execte SQL text, open a table, or open
  85.                 ' a recordset from a file.
  86.                 .CommandType = adCmdStoredProc
  87.                 .CommandText = vbQuote & "Sales By Year" & vbQuote
  88.                 ' The fastest way to execute the stored proc.
  89.                 ' is to append any parameters to the parameters collection.
  90.                 ' However, you have to know all of the required info about parameters
  91.                 ' to do this.
  92.                 Set adoParm = .CreateParameter("@Beginning_Date", adDBTimeStamp, adParamInput, , #8/1/1996#)  ' ***
  93.                 .Parameters.Append adoParm ' ***
  94.                 Set adoParm = .CreateParameter("@Ending_Date", adDBTimeStamp, adParamInput, , #8/15/1996#)  ' ***
  95.                 .Parameters.Append adoParm ' ***
  96.                 ' The next line connects us to the database. Notice that
  97.                 ' we don't have to create a Connection object. Instead, we just use
  98.                 ' the connection string. We are re-establishing the connection
  99.                 ' each time we execute the procedure. Caching the connection (in
  100.                 ' a global variable for example) or enabling ODBC connection
  101.                 ' pooling would speed query execution.
  102.                 .ActiveConnection = ADOConnectionString
  103.                 ' Uncomment the following two lines (and comment out the starred (***)
  104.                 ' lines above) to have the parameters collection
  105.                 ' "build" itself
  106. '                 .Parameters.Refresh
  107. '                 .Parameters("@Beginning_Date") = #8/1/1996#
  108. '                 .Parameters("@Ending_Date") = #8/15/1996#
  109.             End With
  110.             
  111.             Counter = Timer()
  112.             
  113.             With adoResults
  114.                 ' A client-side cursor will allow us to disconnect the recordset
  115.                 ' later.
  116.                 .CursorLocation = adUseClient
  117.                 .Open adoCommand, , adOpenStatic, adLockBatchOptimistic
  118.                 
  119.                 Counter = Timer() - Counter
  120.                 ' Disconnect the recordset to conserve database resources.
  121.                 ' Data is cached locally, but no connection to the database
  122.                 ' exists. You can reconnect the recordset later by setting the
  123.                 ' ActiveConnection property again.
  124.                 Set .ActiveConnection = Nothing
  125.                 
  126.                 ' Call function to display data.
  127.                 DisplayRecordset adoResults
  128.                 
  129.                 .Close
  130.             End With
  131.                   
  132.             Set adoCommand = Nothing
  133.             
  134.         Case ConnectionType.DAO
  135.         
  136.             Dim wrkODBC As DAO.Workspace
  137.             Dim daoConnection As DAO.Connection
  138.             Dim daoQueryDef As DAO.QueryDef
  139.             Dim daoResults As DAO.Recordset
  140.             
  141.             ' Create an ODBCDirect workspace and connect to the database.
  142.             Set wrkODBC = CreateWorkspace("NewODBCWorkspace", "admin", "", dbUseODBC)
  143.             Set daoConnection = wrkODBC.OpenConnection("Northwind", , False, DAOConnectionString)
  144.         
  145.             Counter = Timer()
  146.         
  147.             ' Create a query def. and assign parameter values.
  148.             ' You need to use the ODBC call syntax with DAO.
  149.             Set daoQueryDef = daoConnection.CreateQueryDef("name", "{ call " & vbQuote & "Sales By Year" & vbQuote & " ( '8/1/1996', '8/15/1996') }")
  150.             
  151.             Set daoResults = daoQueryDef.OpenRecordset
  152.             
  153.             Counter = Timer() - Counter
  154.             DisplayRecordset daoResults
  155.             
  156.             ' Clean up.
  157.             daoResults.Close
  158.             daoQueryDef.Close
  159.             daoConnection.Close
  160.             wrkODBC.Close
  161.             
  162.             Set daoResults = Nothing
  163.             Set daoQueryDef = Nothing
  164.             Set daoConnection = Nothing
  165.             Set wrkODBC = Nothing
  166.             
  167.     End Select
  168.             
  169.     txtResults = txtResults.Text & vbCrLf & vbCrLf & Counter & " seconds elapsed."
  170.             
  171. End Sub
  172. Private Sub Form_Load()
  173.     optConnectionType(ConnectionType.ADO).Value = Not SelectedConnectionType
  174.     optConnectionType(ConnectionType.ADO + 1).Value = SelectedConnectionType
  175. End Sub
  176. Private Sub Form_Resize()
  177.     If Me.Width > 2160 Then
  178.         txtResults.Width = Me.Width - 630
  179.     Else
  180.         Me.Width = 2160
  181.     End If
  182.     If Me.Height > 3600 Then
  183.         txtResults.Height = Me.Height - 2685
  184.     Else
  185.         Me.Height = 3600
  186.     End If
  187. End Sub
  188. Private Sub Form_Unload(Cancel As Integer)
  189.     End
  190. End Sub
  191. Private Sub DisplayRecordset(ByVal RecordsetToDisplay As Object)
  192.     Dim objField As Object
  193.     Dim HeaderString As String
  194.     Dim DataString As String
  195.     txtResults.Text = ""
  196.     HeaderString = ""
  197.     DataString = ""
  198.     For Each objField In RecordsetToDisplay.Fields
  199.         HeaderString = HeaderString & objField.Name & vbTab
  200.     Next ' objField
  201.     HeaderString = HeaderString & vbCrLf
  202.     While Not RecordsetToDisplay.EOF
  203.         For Each objField In RecordsetToDisplay.Fields
  204.             DataString = DataString & objField.Value & vbTab
  205.         Next ' objField
  206.         DataString = DataString & vbCrLf
  207.         RecordsetToDisplay.MoveNext
  208.     Wend
  209.     txtResults.Text = HeaderString & DataString
  210. End Sub
  211.