Skip to content

Database Activities

Version: 1.0.0  ·  ID: zoan-packages-database  ·  Author: Zoan Software

The Database package allows your automation to read and modify databases directly with SQL. It is the most efficient way to work with large volumes of data when you have access to the database: query thousands of records, insert results, update statuses. Supports SQL Server, PostgreSQL, MySQL and any source via ODBC.

Working with a database follows the open → use → close pattern:

  1. DB Connect opens a connection and returns a session (ZoanDbSession).
  2. You run one or more queries with DB Query, DB Execute, or DB Scalar.
  3. DB Close closes the connection.

As with other session packages, there are two ways:

Section titled “Option A — DB Connect with body (recommended)”

Activities within body inherit the connection automatically (you don’t configure session), and the connection closes itself upon completion.

DB Connect provider = sqlserver connectionString = = credential("db-ventas").ToPlainText()
└─ body:
DB Query sql = "SELECT * FROM Clientes WHERE Activo = 1" → output: clientes
DB Execute sql = "UPDATE Procesos SET UltimaCorrida = GETDATE()"

DB Connect saves the session to a variable; each activity receives it in session, and you close with DB Close.

DB Connect provider = postgres connectionString = = credential("db").ToPlainText() → output: db
DB Query session = = db sql = "SELECT * FROM pedidos" → output: pedidos
DB Close session = = db
providerMotor
sqlserverMicrosoft SQL Server
postgresPostgreSQL
mysqlMySQL / MariaDB
odbcAny source with ODBC driver

The connection string (connectionString) is the ADO.NET boilerplate that indicates server, database, and credentials:

Server=db.empresa.com;Database=ventas;User Id=bot;Password=...;

Consultas parametrizadas: @nombre + params

Section titled “Consultas parametrizadas: @nombre + params”

Never put variable values directly into the SQL text by concatenating. Instead, use parameters: put @nombre in the query and pass the values ​​in the params field (a { nombre: valor } object).

DB Query
sql = "SELECT * FROM Clientes WHERE Ciudad = @ciudad AND Activo = @activo"
params = { "ciudad": = ciudadBuscada, "activo": true }
→ output: resultado
ActivityPurposeReturns
DB QuerySELECT returning multiple rowsDataTable
DB ScalarSELECT which returns a single value (a COUNT, a MAX)the value
DB ExecuteINSERT / UPDATE / DELETEnumber of rows affected
ActivityTypeOutputWhat it does
DB Connectdb-connectZoanDbSessionOpen a connection and return a session
DB Querydb-queryDataTableRun a SELECT and return the rows
DB Scalardb-scalarobjectPerforms a SELECT and returns a single value
DB Executedb-executeintExecute INSERT/UPDATE/DELETE; returns affected rows
DB Closedb-closeClose the connection

Read pending orders and mark them as processed:

DB Connect provider = sqlserver connectionString = = credential("db-erp").ToPlainText()
└─ body:
DB Query sql = "SELECT * FROM Pedidos WHERE Estado = @e" params = { "e": "pendiente" } → output: pedidos
For Each items = = pedidos itemVariable = fila
└─ activities:
... procesar ...
DB Execute sql = "UPDATE Pedidos SET Estado = 'procesado' WHERE Id = @id" params = { "id": = fila["Id"] }