Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2005
    Posts
    74

    Unanswered: COnnect VB6 to Sql Server

    Hi! can anybody pls. help me. How Can i connect my VB6 Application to Sql server? is it posible to connect my VB6 application directly to sql server without any requirements in the client? thanks in advance!!!

  2. #2
    Join Date
    May 2006
    Posts
    5
    Declarative stuff:

    Code:
    'DSNless stuff
    Private mobjConnect   'The object containing the connection.. declare it at whatever level you need.
    ' or you could specifically type it so you can see all the properties and
    ' methods assciated with in VBPrivate mobjConnect As ADODB.Connection
    
    'These are the variables you'll need for connection or if you're a savage 
    ' you can hard code them in to the method.
    Private DB_NAME As String
    Private SERVER_NAME As String
    Private DB_USER As String
    Private DB_PASS As String
    Then as a property in the db class or just create it as a standard function:

    Code:
    Friend Property Get dbConnect() As Variant
    'Private function GetdbConnect as variant
      'Attempt to get a connection object.
      If mobjConnect = Empty Or Not IsObject(mobjConnect) Then
        '----------------------------
        ' Don't check if there was an error
        'Let the error permeate back to the calling object to report the error.
        '----------------------------
        Set mobjConnect = CreateObject("ADODB.Connection")
        'or use Dim objTmpConnect As New ADODB.Connection
        mobjConnect.ConnectionTimeout = 90
        
        If mbolWinAuth Then
          'Use trusted
          mobjConnect.ConnectionString = "Provider=SQLOLEDB" + ";SERVER=" + SERVER_NAME + ";DATABASE=" + DB_NAME + _
                             ";Trusted_Connection=Yes;APP=MyApp" & App.Major & "." & App.Major & "." & App.Revision
        Else
          'Use application or client defined
          mobjConnect.ConnectionString = "Provider=SQLOLEDB" & ";SERVER=" & SERVER_NAME & ";DATABASE=" & _
                                      DB_NAME & ";User ID=" & DB_USER & ";Password=" & DB_PASS
        End If
      
        ' open connection
        mobjConnect.Open
      End If
      
      Set dbConnect= mobjConnect 'Return ref to connection object
    End Property
    So you then have this dbConnect object (or a reference to it) that you can use to execute SQL/sp's:

    Code:
    Dim objRS
      Set objRS = CreateObject("ADODB.Recordset")
      Set objRS = dbConnect.Execute("Select blah from blah")
    
      If Not objRS.EOF Then
        ??? = objRS.GetRows
     'or 
       ??? = objRS.Fields("column_Name").Value
       objRS.movenext
    '..etc
    Thats basically it...

    Cheers
    Phil
    ---
    Some days you're the bug, some days you're the windscreen.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •