ETLBox comes with a fair set of ControlFlow Tasks that allow you to most common task on a relational database. This will give you an overview of these tasks.
There are a lot of tasks that can help you to create, drop or check the existence of database objects.
In the following there will be example how to create tables, views, procedures, indexes or databases, how to drop them and
how to check for their existence.
CreateTableTask will help you to create a table on the database. You can pass either a TableDefinition object
or a table name and a list of table columns.
Here is an example with passing the table name and a list of table columns
A table column describe the column in the database with the most used attributes: Name, Data type (use the data type
which you are most familiar with, there will be an attempt to convert the database type into the right database specific format),
if the column is nullable, if the column is a primary key and if the column is used as identity column. (Serial for Postgres or
auto increment for MySql). Additionally, you could specify if the column is a computed column or if it has a default value.
Here is an example for creating a TableDefinition and pass it to the CreateTableTask:
Implicit data type conversions:
CreateTableTask will automatically try to convert the given data type into a type that your database actually understands. E.g., if you pass “DATETIME” as data type for a column, and you want to create your table on a Postgres connection manager, CreateTableTask will automatically convert “DATETIME” into “TIMESTAMP”.
Adding your own data type conversion
If you need to change the default data type conversion, you can overwrite it with your implementation. To achieve this, you
need to pass an object that implements IDataTypeConverter. The interface has one method: TryConvertDbDataType and 2 parameter. dbSpecificTypeName is the name that you define in your TableColumn, and connection type defines the connection manager type (e.g. SqlServer or Oracle).
The return value is the converted data type.
Here is an example implementation:
If you are interesting in retrieving a TableDefinition object from an existing database table, use can use
the static method FromTableName on the TableDefinition class:
You can also directly pass a type of an object or class, and ETLBox will try it’s best to create the best suitable TableDefinition for this object.
The returned TableDefinition will vary for different database types.
Retrieving the connection without catalog or database#
In some cases, you might want to get a connection string without a catalog, e.g. because you need to create the database first.
This is where you could use the ConnectionString-Wrapper for you database. E.g., for Postgres you could run the following code:
Your connection manager would now connect to the “postgres” database (which would be the “master” database in Sql Server and “mysql” database
in MySql).
This is the swiss-army knife for running sql on your database. It will use the underlying ADO.NET connection manager,
which allows you to do almost everything on the database, without the “overhead” and boilerplate code that ADO.NET brings with it.
SqlTask always expects a descriptive name when you use it - this name is used for logging purposes.
ExecuteNonQuery will just execute the sql code without reading any results from the database.
Use the following code to read a result set from your database and store it in a List object.
The table to read from would have two columns (ColumnA and ColumnB), and the object MyRow would have two properties (Col1 and Col2).
Bulk inserts in ADO.NET normally need an object which implement IDataReader. Normally, you use a DataTable for this purpose.
But as the implementation of the ADO.NET DataTable has a large overhead and comes with some performance downside, ETLBox
provides it’s own object that implements IDataReader: TableData can be used to be passed to a bulk insert operation.
You can use the BulkDelete method exposed on the SqlTask to perform a batch delete on the database.
It allows you to delete data from a table by passing a TableData object which contains the columns names and the values of each column which you would like to delete.
The SqlTask will then exceute a single DELETE statement on your database that removes all matching records in one execution.
Here is an example:
This will delete the first two rows (with Id 1 and 2), because the value in the columns ‘Id’ and ‘Value1’ matched with the provided data.