Database Activities
Version: 1.0.3 · ID: zoan-packages-database · Author: Zoan Software
The Database package lets your automation 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. It supports SQL Server, PostgreSQL, MySQL and any source via ODBC.
The model: connect → query → close
Section titled “The model: connect → query → close”Working with a database follows the open → use → close pattern:
- DB Connect opens a connection and returns a session (
ZoanDbSession). - You run one or more queries with DB Query, DB Execute or DB Scalar.
- DB Close closes the connection.
As with other session packages, there are two ways:
Option A — DB Connect with body (recommended)
Section titled “Option A — DB Connect with body (recommended)”Activities inside the body inherit the connection automatically (you don’t set session), and the connection closes by itself when it ends.
DB Connect provider = sqlserver connectionString = credential("db-sales")["value"] └─ body: DB Query sql = "SELECT * FROM Customers WHERE Active = 1" → output: customers DB Execute sql = "UPDATE Jobs SET LastRun = GETDATE()"Option B — Session in a variable
Section titled “Option B — Session in a variable”DB Connect stores the session in a variable; each activity receives it in session, and you close it with DB Close.
DB Connect provider = postgres connectionString = credential("db")["value"] → output: dbDB Query session = db sql = "SELECT * FROM orders" → output: ordersDB Close session = dbProviders and connection string
Section titled “Providers and connection string”provider | Engine |
|---|---|
sqlserver | Microsoft SQL Server |
postgres | PostgreSQL |
mysql | MySQL / MariaDB |
odbc | Any source with an ODBC driver installed on the robot’s machine |
In expressions the aliases mssql, sql server, postgresql, pg and mariadb are accepted too. Each engine uses its .NET driver (Microsoft.Data.SqlClient, Npgsql, MySqlConnector); for odbc you install the driver on the robot’s machine.
The connection string (connectionString) is the standard ADO.NET text that sets server, database and credentials:
Server=db.example.com;Database=sales;User Id=bot;Password=...;Parameterized queries: @name + params
Section titled “Parameterized queries: @name + params”Never put variable values straight into the SQL text by concatenating. Use parameters instead: put @name in the query and pass the values in the params field (an object { name: value }).
DB Query sql = "SELECT * FROM Customers WHERE City = @city AND Active = @active" params = { "city": targetCity, "active": true } → output: resultHow parameters are sent
Section titled “How parameters are sent”- Each
paramskey is sent as@key; you can write the key with or without@. - Values can be expressions (
customerId); text, numbers,true/falseandnullreach the engine with their type. - With
odbc, the package replaces each@namein the SQL with?in order of appearance: a repeated name sends its value again and a name with no value inparamsis sent asNULL. Avoid writing@inside SQL string literals when you use ODBC. - Parameters are for values, not for table or column names. If a table name comes from a variable, validate it against a list of allowed values before building the SQL.
Transactions and timeouts
Section titled “Transactions and timeouts”- Autocommit: each statement is committed as it runs; the package does not open transactions. To make several statements apply all or nothing, send them in the same DB Execute inside the engine’s transaction (
BEGIN TRAN … COMMITin SQL Server,BEGIN; … COMMIT;in PostgreSQL) or use a stored procedure. - Timeouts: the activities have no timeout parameter; the driver’s apply. Adjust them in the connection string with the driver’s keywords if you need to.
- Errors: engine errors (syntax, constraints, locks, connection) arrive with the database’s original message. Combine them with Try / Catch and Retry.
Three ways to query
Section titled “Three ways to query”| Activity | Purpose | Returns |
|---|---|---|
| DB Query | SELECT returning several rows | DataTable (0 rows if nothing matches) |
| DB Scalar | SELECT returning a single value (a COUNT, a MAX) | the value (null if there are no rows) |
| DB Execute | INSERT / UPDATE / DELETE | number of affected rows |
Activities
Section titled “Activities”| Activity | Type | Output | What it does |
|---|---|---|---|
| DB Connect | db-connect | ZoanDbSession | Opens a connection and returns a session |
| DB Query | db-query | DataTable | Runs a SELECT and returns the rows |
| DB Scalar | db-scalar | object | Runs a SELECT and returns a single value |
| DB Execute | db-execute | int | Runs INSERT/UPDATE/DELETE; returns affected rows |
| DB Close | db-close | — | Closes the connection |
A typical flow
Section titled “A typical flow”Read pending orders and mark them as processed:
DB Connect provider = sqlserver connectionString = credential("db-erp")["value"] └─ body: DB Query sql = "SELECT * FROM Orders WHERE Status = @s" params = { "s": "pending" } → output: orders For Each items = orders itemVariable = row └─ activities: ... process ... DB Execute sql = "UPDATE Orders SET Status = 'processed' WHERE Id = @id" params = { "id": row["Id"] }Next steps
Section titled “Next steps”- DB Connect — open the connection.
- DB Query — the most common query.
- DataTable — process the results of a SELECT.