Retrieving ADO errors using Delphi

心已入冬 提交于 2019-12-04 08:15:20

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.

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