Log Onto SQL Server Using Windows NT Authentication

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 BINNResources1033SQLDMO.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 BooleanOn Error GoTo LogOnToSqlServer_ErrorHandlerDim objSQLServer As SQLDMO.SQLServer' Create an instance of SQL Server objectSet objSQLServer = New SQLDMO.SQLServer' If this is true Connect to the SQLServer object uses' Windows NT Authentication ModeobjSQLServer.LoginSecure = blnUseNTLogin' Attempt to establish a connection with the SQL ServerobjSQLServer.Connect strServerName, strUserID, strPassword' Return true as call to Connect passedLogOnToSqlServer = TrueExit FunctionLogOnToSqlServer_ErrorHandler:LogOnToSqlServer = False' Display error messageMsgBox "Error Number = " & Err.Number & ", Description = " & Err.Description, _vbCritical, "SQL Server Error"End Function


Share the Post: