DB2 10.5 for Linux, UNIX, and Windows

DB2Connection.EndChain 方法

标记要发送至数据库服务器的一系列 INSERT、UPDATE 和 DELETE 语句的结束。

名称空间:
IBM.Data.DB2
组合件:
IBM.Data.DB2(在 IBM.Data.DB2.dll 中)

语法

[Visual Basic]
Public Sub EndChain()
[C#]
public void EndChain();
[C++]
public: void EndChain();
[JScript]
public function EndChain();

注释

要执行自从调用 BeginChain 方法以来所累积的所有语句,应调用 EndChain 方法。

示例

[Visual Basic, C#] 以下示例使用链接来将 10000 行插入 STAFF 表中。

[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();