DBConnection Data Type

Official Content
This documentation is valid for:

Allows the configuration options for the connections to the database to be determined in execution time.

Description

In order to be able to modify the configuration options of the different data stores, from a GeneXus object and in execution time, a DBConnection data type variable must be defined. To do this, you associate this variable with a specific connection (a GeneXus data store) using the GetDataStore function, and then you manage this connection using the available properties and methods.

Notes

  • In Web Environments only applies using Before Connect property.  
    Database connections in Web objects are handled using a pool, so is not allowed to change it at runtime. Therefore it can only be used within the Procedure referenced in the previous property.
  • For Command Line Procedures it must be taken into account that the Connect()/Disconnect() methods close the cursors which are opened in the connection they apply to. Because of this, it is not possible to execute the Connect() and/or Disconnect() method within the scope of a For each command. The developer is responsible for using the connection/disconnection at the right moment. E.g.: if the disconnection is done in the middle of a procedure, the execution may be canceled. There is also the possibility of continuing working if the procedure is reconnected, but the results will be unpredictable.

Properties

ConnectionData ODBCDatasourceName
ConnectionMethod Property ODBCDriverName
DatastoreName ODBCFileDatasourceName
ErrCode ShowPrompt
ErrDescription UseExternalDatasource
ExternalDatasourceName UserName
JDBCDriverName UserPassword
JDBCDriverURL  

Methods

Connect Disconnect

Important: If the Before Connect property is set, the Connect() method execute that procedure, except when it is referenced within that object.

Examples

  • ODBC Connection
    &MyConnection is a variable of DBConnection type
<gx source="" start="">
    &MyConnection = GetDatastore("default")
    &MyConnection.Disconnect()
    &MyConnection.UserName = "Administrator"
    &MyConnection.UserPassword = "Adm!Password"
    &MyConnection.ShowPrompt = 2
    &MyConnection.Connect()
</gx>
  • JDBC Connection

&MyConn is a variable of DBConnection type

Event 'Connect'
      &myConn = GetDataStore("Default")
      &myConn.UserName = trim(&User)
      &myConn.UserPassword = trim(&Password)
      &myConn.JDBCDriverName='com.inet.tds.TdsDriver'
      &myConn.JDBCDriverURL='jdbc:inetdae:barbanegra:1433?database=testemp2'
      &myConn.ShowPrompt = 2
      &Res = &myConn.Connect()
      Do case
        Case &Res = 0
              &ConnOk = 'Y'
              return
         Otherwise
              &ConnOk = 'N'
              msg('Unsuccessful connection, retry...')
      Endcase
EndEvent  // 'Connect'

Scope

Objects: Procedures, Transactions, Web Panels
Languages: .Net, Java

Security Tips

-Avoid setting external user's data to the properties or methods, or sanitize accordingly the entries

See Also

GetDataStore Function


Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.