Tuesday, February 14, 2012

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!!!Declarative stuff:

'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:

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:

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.

No comments:

Post a Comment