Skip to content

LogIn

See Also: LogOut, DF_File_Login, Server_Name INT file keyword

Purpose

To log into a database server.

Syntax

LogIn {server-name} {user-name} {password} [{driver-name}]

Argument Explanation

  • serverName: Name of the server to be logged into.
  • userName: Name of the user for the login.
  • password: Password to use for the login.
  • driverName: Optional database driver to use for the login. If not used, a login will be attempted for all drivers. Can be one of:
  • DB2_DRV: DB2 Driver
  • DFBTRDRV: Pervasive.SQL Driver
  • MSSQLDRV: SQL Server Driver
  • ODBC_DRV: ODBC Driver

What It Does

This command is for use in database-server architectures to allow the user to log into a named server. If the combination of serverName, userName, and password is correct, a connection will be established for the server. If the combination is incorrect, an error will be generated. An application can trap these errors and allow additional login attempts.

If password is null, the login will only succeed if you have no password set for the server(s) specified.

Example

LogIn "DSN=accounts" "johndoe" "jane" "ODBC_DRV"

This command will log into an ODBC database with a DSN named accounts, user id "johndoe", and password "jane".

In addition, the .INT file for each table that will be accessed via DSN accounts needs to have the following entries:

DRIVER_NAME ODBC_DRV
SERVER_NAME DSN=accounts

For example, this command can be used in the Procedure OnCreate of a cApplication object for an application:

Procedure OnCreate
    Send DoOpenWorkspace CURRENT$WORKSPACE
    LogIn "DSN=accounts" "johndoe" "jane" "ODBC_DRV"
End_Procedure

The hard-coded user id and password can also be requested from the user via a login dialog and used with the login as variables.

Logging in Using a Trusted Connection

To log in to an SQL Server database using a trusted connection, use the following:

LogIn "SERVER=MyServerName;DATABASE=MyDatabaseName;Trusted_Connection=yes" "" "" "MSSQLDRV"

In addition, the .INT file for each table that will be accessed this way needs to have the following entries:

SERVER_NAME SERVER=MyServerName;DATABASE=MyDatabaseName

Where to Place the Login Command

In a typical DataFlex Windows application where a login is required just at the launch of the application, you can place the login command statement just prior to the Start_UI statement.

In a typical DataFlex Web application where a login is required just at the launch of the application, you can place the login command statement just prior to the send StartWebApp statement.

Intercepting a Failed Login

Depending on the database you are using, security will be somewhat different. In most cases, if security for the database is activated, if the login information you are passing to the database via the login command is incorrect, the database will pop up a login dialog to request a user id and password. If the login fails, you have to ensure in your code that your application does not open and allow access to the database even though the login failed.

Here is an example of how you can intercept a failed login:

// This is procedure OnClick of a button in a login dialog
// The login dialog has 2 Form objects that accept a user id and password
Procedure OnClick
    String sUserId sPW
    Handle hoErrorObj
    Get Value of oUserId to sUserId
    Get Value of oPassword to sPW
    // Change the global error object to the current object
    // This allows interception of errors from Login command via Procedure Error_Report below
    Move Error_Object_Id to hoErrorObj
    Move Self to Error_Object_Id
    LogIn "DSN=pkb" sUserId sPW "ODBC_DRV"
    // Change the global error object back to what it was before we intercepted it
    Move hoErrorObj to Error_Object_Id
    // Error 12293 is "Login unsuccessful"
    If (LastErr = 12293) Begin
        Send Stop_Box "Login Unsuccessful" "Error"
        Send Close_Panel  // Close password dialog
        Send Exit_Application of oMain  // Close application
    End
End_Procedure // OnClick

// This method is here simply to intercept any error that may occur while logging in
// It does not have to do anything with the error, but it suppresses the normal error display
Procedure Error_Report Integer iErrNum Integer iErrLine String sErrMsg
End_Procedure  // Error_Report