On this page
dotConnect for Snowflake sends data modification statements to Snowflake as is. Snowflake executes them. You can modify data in the same way as with any other ADO.NET provider: execute DML statements with a SnowflakeCommand, or change the data in a DataTable and call the Update method of a SnowflakeDataAdapter. For details about the execution of DML statements, see Execution of DML Statements.
Transactions
dotConnect for Snowflake supports Snowflake transactions. To start a transaction, call the SnowflakeConnection.BeginTransaction method. To end it, call the Commit or Rollback method of the returned SnowflakeTransaction object.
- A transaction that is disposed without calling Commit is rolled back. If the connection is closed or returned to the pool while a transaction is open, the transaction is rolled back too.
- A connection can have only one open transaction at a time.
- Snowflake supports only the READ COMMITTED isolation level. BeginTransaction throws NotSupportedException if the isolation level is neither Unspecified nor ReadCommitted.
- Snowflake has no savepoints.
Snowflake implicitly commits the open transaction before it executes a DDL statement (CREATE, ALTER, DROP, and so on, including CREATE TEMPORARY TABLE). After that, Rollback doesn't undo the changes made before the DDL statement, and the following statements run in the autocommit mode. Create and drop database objects before you start a transaction or after you end it. TRUNCATE TABLE doesn't commit the transaction: it is rolled back together with the other statements of the transaction. For more information, see Transactions in the Snowflake documentation.
When autocommit is enabled (the Snowflake default), Snowflake automatically commits each successful statement executed outside an explicit transaction.
The following example demonstrates how to execute multiple data modification statements within a single transaction. If an exception occurs before the call to Commit, the provider rolls back the transaction when the transaction object is disposed. The example uses the dept table (see Updating Data) and the emp table, which you can create with the following statements:
|
|---|
CREATE TABLE emp (empno INT, ename VARCHAR(10), sal NUMBER(7, 2), deptno INT);
INSERT INTO emp VALUES (7369, 'SMITH', 800, 20), (7499, 'ALLEN', 1600, 30), (7521, 'WARD', 1250, 30), (7839, 'KING', 5000, 10); |
|
|---|
using (SnowflakeTransaction transaction = connection.BeginTransaction()) {
using (SnowflakeCommand command = new SnowflakeCommand(
"UPDATE dept SET loc = :loc WHERE deptno = :deptno", connection)) {
command.Transaction = transaction;
command.Parameters.Add("loc", DbType.String).Value = "BOSTON";
command.Parameters.Add("deptno", DbType.Int64).Value = 20;
command.ExecuteNonQuery();
}
using (SnowflakeCommand command = new SnowflakeCommand(
"DELETE FROM emp WHERE deptno = :deptno", connection)) {
command.Transaction = transaction;
command.Parameters.Add("deptno", DbType.Int64).Value = 30;
command.ExecuteNonQuery();
}
transaction.Commit();
}
|
|
|---|
Using transaction As SnowflakeTransaction = connection.BeginTransaction()
Using command As New SnowflakeCommand( _
"UPDATE dept SET loc = :loc WHERE deptno = :deptno", connection)
command.Transaction = transaction
command.Parameters.Add("loc", DbType.String).Value = "BOSTON"
command.Parameters.Add("deptno", DbType.Int64).Value = 20
command.ExecuteNonQuery()
End Using
Using command As New SnowflakeCommand( _
"DELETE FROM emp WHERE deptno = :deptno", connection)
command.Transaction = transaction
command.Parameters.Add("deptno", DbType.Int64).Value = 30
command.ExecuteNonQuery()
End Using
transaction.Commit()
End Using
|
A transaction is associated with the Snowflake session used by the connection. If the session ends during an active transaction, the transaction is lost, and its uncommitted changes are rolled back.
The provider doesn't automatically retry requests that fail because of network errors during an active transaction, because Snowflake might have already executed the statement.
In both cases, the provider throws an exception. If the error occurs during Commit, the changes might already be committed: check the data before you run the transaction again. Otherwise, roll the transaction back and run it again.
To reduce the risk of session expiration during a transaction, keep transactions short and avoid leaving them open while waiting for user input.
For more information about handling transaction errors, see Troubleshooting.
TransactionScope
The TransactionScope class is supported:
- A connection that is opened inside a scope joins it automatically, because the Enlist connection string parameter is True by default. A connection that is already open can join the scope with the EnlistTransaction method. If you set Enlist to False, the connection ignores the scope.
- If the scope is completed, the transaction is committed; otherwise, it is rolled back.
- Snowflake doesn't support distributed transactions. A scope can include only one SnowflakeConnection and no other resources that need a distributed transaction: the provider refuses to promote the transaction.
- Inside a scope, you can't start a local transaction with the BeginTransaction method.