Create a connection object with a transaction
- Last UpdatedJul 22, 2024
- 1 minute read
SQLData scripts follow a typical flow written with transactions:
-
Create a connection object.
-
Create a transaction object by using the CreateTransaction() method of the connection object.
-
Add one or more command objects to the transaction by using the CreateCommand() method of the transaction object.
-
For each command object whose SQL statement contains parameters, initialize each parameter by using either the Set<Type>ParameterByName() method or the Set<Type>ParameterByIndex() method of the command object.
-
Run the transaction object either synchronously or asynchronously.
If any of the commands fails or returns an error, the transaction and any commands that ran within the transaction are rolled back. The transaction is returned with the appropriate indicator
When transaction processing is complete, you can retrieve and modify the returned dataset for each command in the transaction that returns data. If you modify a dataset, you can save it back to the database either synchronously or asynchronously.
You can reuse transaction objects indefinitely. When finished, clean up the transaction object and command objects by calling their Dispose() methods.