Retrieving ADO errors using Delphi

岁酱吖の 提交于 2019-12-06 03:29:36

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/1457032/retrieving-ado-errors-using-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!