DB2Connection.BeginChain 方法
标记要发送至数据库服务器的一系列 INSERT、UPDATE 和 DELETE 语句的开始。
- 名称空间:
IBM.Data.DB2- 组合件:
IBM.Data.DB2(在IBM.Data.DB2.dll中)
语法
[Visual Basic]
Public Sub BeginChain()
[C#]
public void BeginChain();
[C++]
public: void BeginChain();
[JScript]
public function BeginChain();
异常
| 异常类型 | 条件 |
|---|---|
| InvalidOperationException | 连接未打开。 |
备注
可通过链接 SQL 语句来提高大型批处理 INSERT、UPDATE 和 DELETE 语句的性能。 链接 SQL 语句可通过降低数据库服务器的网络流量来提高应用程序性能。
在调用 BeginChain 方法以激活 SQL 语句的链接之前,必须打开 DB2Connection 对象。 调用 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 状态,并单独向数据库服务器提交所有语句。 您可以通过检查 链 属性来确定 SQL 语句的链是否处于活动状态。
对于单个 DB2Connection 对象,可以链接的语句数没有限制。 但是,在 2,147,483,646 个语句排队之后, IBM Data Server Provider for .NET 会在内部关闭链,提交语句,然后重新启动链。 另外,一旦应用程序与数据库服务器之间的通信缓冲区(通常为 32KB)已满,就会将该缓冲区中的内容发送至服务器并保存在服务器中,直到调用 EndChain 为止。 可以使用 rqrioblk 配置参数来调整此通信缓冲区的大小。
为了获得最佳性能,可在 INSERT、UPDATE 和 DELETE 语句中使用参数标记。
您不能在连接到 Db2 for z/OS 服务器的连接中不能链入 XA 事务。
示例
[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();