使用 Delphi 检索 ADO 错误

发布于 2024-08-05 20:55:38 字数 344 浏览 2 评论 0原文

我使用 Delphi 2007 和 ADO 来访问 SQL Server 2008 数据库。

数据库上的存储过程会预先验证输入,如果验证失败,则会返回错误结果集(包含自定义错误信息)。使用 SQL Server Management Studio,当我运行存储过程时,我会在一个选项卡中获取自定义错误结果集,并在另一个选项卡中获取本机错误消息。

回到我的 Delphi 应用程序,当我打开存储过程时,我可以访问自定义错误结果集。但是,ADO 连接上的 Errors 对象不包含本机错误。

如何访问 Errors 集合对象以便提供有关错误原因的更多信息?

谢谢

I'm using Delphi 2007 with ADO to access a SQL Server 2008 database.

A stored procedure on the database prevalidates the input and if the validation fails it returns an error result set (containing custom error info). Using SQL Server Management Studio, when I run the stored procedure, I get the custom error result set in one tab and the native error message in another.

Back in my Delphi app, when I open the stored procedure, I can access the custom error result set. However, the Errors object on the ADO connection does not contain the native error.

How do I access the Errors collection object so I can provide more information about the cause of the error ?

Thanks

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

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

发布评论

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

评论(1

述情 2024-08-12 20:55:38

选项 1) 使用 ADO 连接错误集合。

try
....
....
....
 ADOQuery1.Open;//Execute your sql statement
except
  LastErrorIndex  :=ADOConnection1.Errors.Count-1;
  SourceError     :=ADOConnection1.Errors.Item[LastErrorIndex].Source;
  NumberError     :=ADOConnection1.Errors.Item[LastErrorIndex].Number;
  DescriptionError:=ADOConnection1.Errors.Item[LastErrorIndex].Description;
  SQLStateError   :=ADOConnection1.Errors.Item[LastErrorIndex].SQLState;
  NativeError     :=ADOConnection1.Errors.Item[LastErrorIndex].NativeError;
end;

选项2)
您可以使用 @@error 变量从 sql server 获取最后一个错误。

select @@error

当Sql Server发生错误时,您只能使用@@ERROR全局变量获取错误号。没有 @@ERROR_MESSAGE 全局变量来获取错误描述。
对于完整的错误消息,可以使用错误号查询master..sysmessages表:

SELECT Description FROM master..sysmessages  WHERE error= @@ERROR AND msglangid=1033

但是这些消息大多数都有占位符(如%s、%ld),你也可以使用这个存储过程

您可以阅读这篇文章SQL Server 中的错误处理 – 背景了解更多信息。

再见。

Option 1) using the ADO Connection Errors collection.

try
....
....
....
 ADOQuery1.Open;//Execute your sql statement
except
  LastErrorIndex  :=ADOConnection1.Errors.Count-1;
  SourceError     :=ADOConnection1.Errors.Item[LastErrorIndex].Source;
  NumberError     :=ADOConnection1.Errors.Item[LastErrorIndex].Number;
  DescriptionError:=ADOConnection1.Errors.Item[LastErrorIndex].Description;
  SQLStateError   :=ADOConnection1.Errors.Item[LastErrorIndex].SQLState;
  NativeError     :=ADOConnection1.Errors.Item[LastErrorIndex].NativeError;
end;

Option 2)
You can use the @@error variable to get the last error from sql server.

select @@error

When an error occurs in Sql Server, all you can get is the error number, using the @@ERROR global variable. There is no @@ERROR_MESSAGE global variable to get the error description.
For a complete error message, you can query the master..sysmessages table using the error number:

SELECT Description FROM master..sysmessages  WHERE error= @@ERROR AND msglangid=1033

but most of these messages have place holders (like %s, %ld), you can also use this Stored Procedure.

you can read this article Error Handling in SQL Server – a Background for more information.

Bye.

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