[英]How to use TransactionScope class to use same connection without starting a transaction?
I have a stored procedure A
that creates a global temporary table (prefixed with ##
) where it inserts some temporary data. 我有一个存储过程A
,该存储过程A
创建一个全局临时表 (前缀##
),并在其中插入一些临时数据。 Then, I have a stored procedure B
that consumes this data. 然后,我有一个使用此数据的存储过程B
Hence, A
and B
must be called during the same session (connection). 因此,必须在同一会话(连接)中调用A
和B
Otherwise the temporary table will be dropped. 否则临时表将被删除。
My database framework constantly use a construct as follows: 我的数据库框架不断使用如下构造:
public T GetSomeData()
{
using (var connection = OpenConnection())
{
// ...
}
}
Now, I need to make sure that A
and B
are called on the same connection . 现在,我需要确保在相同的连接上调用A
和B
I can easily accomplish that with the TransactionScope class, but that also implies a transaction which I do not want. 我可以使用TransactionScope类轻松完成此操作,但这也意味着我不想要的事务。
using (var transaction = new TransactionScope())
{
A();
// Now B can read the temporary table created by A.
// However, a transaction has been started which causes problems!
B();
// I don't want a transaction...
// transaction.Complete();
}
My question is: How can I use this design pattern to create a "connection scope"? 我的问题是: 如何使用这种设计模式创建“连接范围”?
Use the same DataReader to read multiple resultsets. 使用相同的DataReader读取多个结果集。
using(var reader = command.ExecuteReader())
{
while(reader.Read())
{
A();
}
reader.NextResult();
while(reader.Read())
{
B();
}
}
The same connection is used to get both resultsets. 相同的连接用于获取两个结果集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.