调用 MS SQL SERVER 2008 R2 时在 delphi 中使用事务时出现问题?

发布于 2024-09-17 09:18:34 字数 1430 浏览 2 评论 0原文

我需要从 Delphi 调用一些存储过程,因为它们是相关的,所以我必须使用事务。
但调用时它总是返回错误:

'事务不能有多个具有此游标类型的记录集。更改游标类型、提交事务或关闭记录集之一。'

这个错误仅发生在 MS SQL SERVER 2008 上,当我使用 MS Access 时它工作正常。
有什么问题吗?

预先感谢

更新:

procedure TForm1.Button2Click(Sender: TObject);  
begin  
    if not DM.ADOConnection.InTransaction then  
        dm.ADOConnection.BeginTrans;  
    ADOQuery.LockType := ltBatchOptimistic;  
    ADOQuery.CursorType := ctUnspecified;  
    Try  
        with ADOQuery do  
        begin  
            Close;  
            SQL.Clear;  
            SQL.Text := 'INSERT INTO [UserAction] (UAct_Frm_ID,UAct_Type,UAct_Description'  
              +',UAct_Date,UAct_Time,UAct_Usr_ID)'
              +'VALUES(:UAct_Frm_ID'
              +',:UAct_Type,:UAct_Description,:UAct_Date,:UAct_Time'
              +',:UAct_Usr_ID)';
  Parameters.ParamByName('UAct_Frm_ID').Value := 1;
  Parameters.ParamByName('UAct_Type').Value := 1;
  Parameters.ParamByName('UAct_Description').Value := 'test by Q1';
  Parameters.ParamByName('UAct_Date').Value := completdate(datenow);
  Parameters.ParamByName('UAct_Time').Value := TimeToStr(Now);
  Parameters.ParamByName('UAct_Usr_ID').Value := 1;
  ExecSQL;
  end;
  Except
    DM.ADOConnection.RollbackTrans;
    ShowMessage('RollBack');
    Exit;
  End;
  dm.ADOConnection.CommitTrans;
  ShowMessage('Commite');
end; 

I need to call some Stored Procedures from Delphi and because they are related I have to use transactions.
But It always returns an error when called :

'Transaction cannot have multiple recordsets with this cursor type. Change the cursor type ,commit the transaction, or close one of the recordsets.'

And this error only occurs for MS SQL SERVER 2008, when I use MS Access It works fine.
Whats the problem ?

Thanks in advance

UPDATE :

procedure TForm1.Button2Click(Sender: TObject);  
begin  
    if not DM.ADOConnection.InTransaction then  
        dm.ADOConnection.BeginTrans;  
    ADOQuery.LockType := ltBatchOptimistic;  
    ADOQuery.CursorType := ctUnspecified;  
    Try  
        with ADOQuery do  
        begin  
            Close;  
            SQL.Clear;  
            SQL.Text := 'INSERT INTO [UserAction] (UAct_Frm_ID,UAct_Type,UAct_Description'  
              +',UAct_Date,UAct_Time,UAct_Usr_ID)'
              +'VALUES(:UAct_Frm_ID'
              +',:UAct_Type,:UAct_Description,:UAct_Date,:UAct_Time'
              +',:UAct_Usr_ID)';
  Parameters.ParamByName('UAct_Frm_ID').Value := 1;
  Parameters.ParamByName('UAct_Type').Value := 1;
  Parameters.ParamByName('UAct_Description').Value := 'test by Q1';
  Parameters.ParamByName('UAct_Date').Value := completdate(datenow);
  Parameters.ParamByName('UAct_Time').Value := TimeToStr(Now);
  Parameters.ParamByName('UAct_Usr_ID').Value := 1;
  ExecSQL;
  end;
  Except
    DM.ADOConnection.RollbackTrans;
    ShowMessage('RollBack');
    Exit;
  End;
  dm.ADOConnection.CommitTrans;
  ShowMessage('Commite');
end; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-09-24 09:18:34

来自此处

分辨率

使用不同的光标类型,更改
adUseClient 的光标位置或
之前关闭第一个记录集
在同一个上打开另一个
连接/交易。

原因

SQL Server 只能打开一个
一次只向前移动光标
连接,因为 SQL Server 可以
只处理一个活动语句
每次连接的时间。

当您尝试打开多个
一次只转发 ADO 记录集
单个连接,仅第一个
ADO 记录集实际上打开于
连接对象。新的、独立的
为后续创建连接
仅向前游标。

一笔交易是在一个单一的
联系。当您尝试打开时
多个 ForwardOnly 记录集
在单个事务中,ADO
尝试打开多个
ForwardOnly 记录集
交易的连接。一个
发生错误是因为仅 SQL Server
允许一个 ForwardOnly 记录集
单一连接。因为错误
在手动交易中,您
可能会看到上面的错误。微软
数据访问对象 2.1 Service Pack 2
以及 MDAC 的更高版本包含
提供更多信息的错误消息。为了
这个原因,你可能会看到更多
信息性第二个或第三个错误
消息,上面。

From here:

Resolution:

Use a different cursor type, change
the cursor location to adUseClient or
close the first recordset before
opening another on the same
connection/transaction.

Cause:

SQL Server can only open one
ForwardOnly cursor at a time on a
connection, because SQL Server can
only process one active statement at a
time per connection.

When you try to open more than one
ForwardOnly ADO recordset at a time on
a single Connection, only the first
ADO recordset is actually opened on
the Connection object. New, separate
connections are created for subsequent
ForwardOnly cursors.

A transaction is on a single
connection. When you attempt to open
more than one ForwardOnly recordset
within a single transaction, ADO
attempts to open more than one
ForwardOnly recordset on the
connection of the transaction. An
error occurs because SQL Server only
allows one ForwardOnly recordset on a
single connection. Because the error
is within a manual transaction, you
might see the error above. Microsoft
Data Access Objects 2.1 Service Pack 2
and later versions of MDAC contain
more informative error messages. For
that reason, you may see the more
informative second or third error
message, above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文