Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Enterprise
Expertise: Intermediate
Jul 13, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Log Onto SQL Server Using Windows NT Authentication

Here is a simple function you can use to connect to SQL Server. This function attempts to connect to the server either using the default SQL Server authentication or Windows NT authentication. If you want to connect to the server using Windows NT authentication, then you can pass "True" for blnUseNTLogin parameter. Internally, this function uses "LoginSecure" property of the SQLDMO.SqlServer object. When "LoginSecure" is set to "True", any values provided for "Login" and "Password" arguments in the Connect method are ignored.

In order to run this function, you need to add a reference to "Microsoft SQLDMO Object Library" (SQLDMO.RLL) in your VB project. You can find this file in \BINN\Resources\1033\SQLDMO.RLL under your SQL Server 7.0 directory.

Private Function LogOnToSqlServer(ByVal strServerName As String, _
ByVal strUserID As String, _
ByVal strPassword As String, _
ByVal blnUseNTLogin As Boolean) As Boolean

On Error GoTo LogOnToSqlServer_ErrorHandler

Dim objSQLServer As SQLDMO.SQLServer

' Create an instance of SQL Server object
Set objSQLServer = New SQLDMO.SQLServer

' If this is true Connect to the SQLServer object uses
' Windows NT Authentication Mode
objSQLServer.LoginSecure = blnUseNTLogin
' Attempt to establish a connection with the SQL Server
objSQLServer.Connect strServerName, strUserID, strPassword
' Return true as call to Connect passed
LogOnToSqlServer = True
Exit Function

LogOnToSqlServer = False
' Display error message
MsgBox "Error Number = " & Err.Number & ", Description = " & Err.Description, _
vbCritical, "SQL Server Error"

End Function
Deepak Pant
Thanks for your registration, follow us on our social networks to keep up-to-date