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
See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist