Attribute VB_Name = "dbConn"
'----------------------------------------------------------------------------
'
' (c) Copyright IBM Corp. 2007 All rights reserved.
' 
' The following sample of source code ("Sample") is owned by International 
' Business Machines Corporation or one of its subsidiaries ("IBM") and is 
' copyrighted and licensed, not sold. You may use, copy, modify, and 
' distribute the Sample in any form without payment to IBM, for the purpose of 
' assisting you in the development of your applications.
' 
' The Sample code is provided to you on an "AS IS" basis, without warranty of 
' any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR 
' IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
' MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do 
' not allow for the exclusion or limitation of implied warranties, so the above 
' limitations or exclusions may not apply to you. IBM shall not be liable for 
' any damages you suffer as a result of using, copying, modifying or 
' distributing the Sample, even if IBM has been advised of the possibility of 
' such damages.
'----------------------------------------------------------------------------
'
' SOURCE FILE NAME: dbConn.bas
'
' SAMPLE: Connect to / disconnect from a database
'
' FORMS USED:
'         frmMain (Demo.frm)
'
'----------------------------------------------------------------------------
'
' For more information on the sample programs, see the README file.
'
' For information on building ADO applications with Visual Basic, 
' see the Developing ADO.NET and OLE DB Applications book.
'
' For information on using SQL statements, see the SQL Reference.
'
' For the latest information on programming, compiling, and running DB2
' applications, visit the DB2 Information Center:
'     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
'----------------------------------------------------------------------------

Option Explicit

'This procedure creates a connection to the sample database by using
'the IBM OLE DB provider for DB2.
Public Function ConnectOLEDB() As ADODB.Connection
  
  'set up connection string
  Dim ConnectString As String
  ConnectString = "Provider=IBMDADB2;DSN=SAMPLE;User Id=;Password=;"

  'open new connection with client side cursor
  Dim con As ADODB.Connection
  Set con = New ADODB.Connection
  con.CursorLocation = adUseClient
  con.Open ConnectString

  'return the connection
  Set ConnectOLEDB = con

  'release the handler
  Set con = Nothing

End Function

'This procedure creates a connection to the sample database by using
'the MS ODBC bridge provider, the sample database must be registered
'with the ODBC administrator.
'NOTE: For the LOBs to work with this ODBC connection,
'      the keyword LONGDATACOMPAT=1
'      must be set in the db2cli.ini file.
Public Function ConnectODBC() As ADODB.Connection
  
  'set up connection string
  Dim ConnectString As String
  ConnectString = "Provider=MSDASQL;DSN=SAMPLE;User Id=;Password=;"

  'open new connection with client side cursor
  Dim con As ADODB.Connection
  Set con = New ADODB.Connection
  con.CursorLocation = adUseClient
  con.Open ConnectString

  'return the connection
  Set ConnectODBC = con

  'release the handler
  Set con = Nothing

End Function

'This procedure creates a connection to the sample database by using
'the IBM OLE DB provider together with Microsoft data shape provider
'so that hierarchical cursors can be obtained.
Public Function ConnectDataShape() As ADODB.Connection
  
  'set up connection string
  Dim ConnectString As String
  ConnectString = "Provider=MSDataShape;" & _
                  "Data Provider=IBMDADB2;" & _
                  "DSN=SAMPLE;User Id=;Password=;"

  'open a new connection
  Dim con As ADODB.Connection
  Set con = New ADODB.Connection
  con.Open ConnectString

  'return the connection
  Set ConnectDataShape = con

  'release the handler
  Set con = Nothing

End Function

'This procedure closes the connection from the sample database.
Public Function Disconnect(con As ADODB.Connection) As String
  
  'close the connection to database
  con.Close
  Set con = Nothing
  
  'return a message
  Disconnect = "Sample database is Disconnected!"

End Function