DB2®Connection.EndChain Method
Marks the end of a chain of insert, update, and delete statements to be sent to the database server.
- Namespace:
IBM.Data.DB2
- Assembly:
IBM.Data.DB2
(inIBM.Data.DB2.dll
)
Syntax
[Visual Basic]
Public Sub EndChain()
[C#]
public void EndChain();
[C++]
public: void EndChain();
[JScript]
public function EndChain();
Remarks
To execute all the statements accumulated since the call to the BeginChain method, call the EndChain method.
Example
[Visual Basic, C#] The following example uses chaining to insert 10000 rows into the STAFF table.
[Visual Basic]
Dim con As DB2Connection = new DB2Connection("DATABASE=sample;")
Dim cmd As DB2Command = con.CreateCommand()
con.Open()
' Initialize an insert statement using parameter markers
cmd.CommandText = "INSERT INTO STAFF(ID) VALUES( ? )"
' Add a parameter
Dim p1 As DB2Parameter = cmd.Parameters.Add("@ID", DB2Type.Integer )
' Start the chain
con.BeginChain()
Try
' Loop to add 10000 rows
Dim I As Int32
For I = 1 To 10000
' Set the parameter value
p1.Value = I
' Execute the command.
' Since chaining is active, this statement is now added
' to the chain
cmd.ExecuteNonQuery()
Next I
' Execute the chain
con.EndChain()
Catch db2Ex As DB2Exception
Dim db2Error As DB2Error
' Loop through all the errors
For Each db2Error in db2Ex.Errors
Console.WriteLine("SQLSTATE =" & db2Error.SQLState )
Console.WriteLine("NativeErr=" & db2Error.NativeError )
Console.WriteLine("RowNumber=" & db2Error.RowNumber )
Console.WriteLine( db2Error.Message )
Next DB2Error
Finally
' Explicitly turn chaining off in case it is still on
If (con.Chaining) Then
con.EndChain()
End If
End Try
con.Close()[C#]
DB2Connection con = new DB2Connection("DATABASE=sample;");
DB2Command cmd = con.CreateCommand();
con.Open();
// Initialize an insert statement using parameter markers
cmd.CommandText = "INSERT INTO STAFF(ID) VALUES( ? )";
// Add a parameter
DB2Parameter p1 = cmd.Parameters.Add("@ID", DB2Type.Integer );
// Start the chain
con.BeginChain();
try
{
// Loop to add 10000 rows
for( Int32 i = 1; i <= 10000; i++ )
{
// Set the parameter value
p1.Value = i;
// Execute the command.
// Since chaining is active, this statement is now added
// to the chain
cmd.ExecuteNonQuery();
}
// Execute the chain
con.EndChain();
}
catch( DB2Exception db2Ex )
{
// Loop through all the errors
foreach( DB2Error db2Error in db2Ex.Errors )
{
Console.WriteLine("SQLSTATE =" + db2Error.SQLState );
Console.WriteLine("NativeErr=" + db2Error.NativeError );
Console.WriteLine("RowNumber=" + db2Error.RowNumber );
Console.WriteLine( db2Error.Message );
}
}
finally
{
// Explicitly turn chaining off in case it is still on
if( con.Chaining )
{
con.EndChain();
}
}
con.Close();