标记要发送至数据库服务器的一系列 INSERT、UPDATE 和 DELETE 语句的开始。
[Visual Basic]
Public Sub BeginChain()
[C#]
public void BeginChain();
[C++]
public: void BeginChain();
[JScript]
public function BeginChain();
| 异常类型 | 条件 |
|---|---|
| InvalidOperationException | 连接未打开。 |
可通过链接 SQL 语句来提高大型批处理 INSERT、UPDATE 和 DELETE 语句的性能。链接 SQL 语句可通过降低数据库服务器的网络流量来提高应用程序性能。
必须打开 DB2Connection 对象,然后才能调用 BeginChain 方法以激活 SQL 语句的链接。 在调用 BeginChain 方法之后,对 DB2Connection 对象运行的 INSERT、UPDATE 和 DELETE 语句将在客户机上排队,直到调用了 EndChain 方法为止。在调用 EndChain 方法之后,会将这些语句发送至数据库服务器。
无论链接是否处于活动状态,都可以调用 DB2CommandClass.ExecuteNonQuery 方法以运行 INSERT、UPDATE 和 DELETE 语句。但是,当链接处于活动状态时,对 DB2Command.ExecuteNonQuery 的所有调用都将返回 -1。这是因为在调用 EndChain 方法之后将同时运行链接在一起的所有语句,所以受特定语句影响的行数是未知的。可以从多个 DB2Command 对象执行链接的语句,也可以从单个 DB2Command 对象运行链接语句。 必须从激活了链接的同一 DB2Connection 对象来创建用于运行链接的一系列语句的所有 DB2Command 对象。
为了与支持链接的数据库服务器进行连接,在调用 BeginChain 方法时将 Chaining 属性设置为 true,而在调用 EndChain方法时又将该属性重置为 false。 如果服务器不支持链接,那么 IBM® Data Server Provider for .NET 将忽略 BeginChain 和 EndChain 请求,并让 DB2Connection.Chaining 属性继续保持为 false,并将所有语句单独提交给数据库服务器。可以通过检查 Chaining 属性来确定 SQL 语句的链接是否处于活动状态。
对于可以为单个 DB2Connection 对象链接的语句数没有任何限制。但是,在 2,147,483,646 个语句进行排队之后,IBM Data Server Provider for .NET 将会在内部关闭链接,提交这些语句,然后重新启动链接。另外,一旦应用程序与数据库服务器之间的通信缓冲区(通常为 32KB)已满,就会将该缓冲区中的内容发送至服务器并保存在服务器中,直到调用 EndChain 为止。可以使用 rqrioblk 配置参数来调整此通信缓冲区的大小。
为了获得最佳性能,可在 INSERT、UPDATE 和 DELETE 语句中使用参数标记。
您可以在连接到 XA 事务中登记的 DB2® for z/OS® 服务器时链接 SQL 语句。
[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();