Getting ADO Errors Using Delphi

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

The stored procedure in the database is dominated by input, and if the test fails, it returns a set of error results (containing error information). Using SQL Server Management Studio, when I start a stored procedure, I get an individual error result in one tab and my own error message in another.

In my Delphi application, when I open a stored procedure, I can access a custom error set. However, the object Errorsin the ADO connection does not contain a built-in error.

How can I access the collection object Errorsso that I can provide additional information about the cause of the error?

thanks

+5
source share
1 answer

Option 1) using a collection of ADO connection errors.

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 latest error on the sql server.

select @@error

If an error occurs on the Sql server, all you can get is the error number using the global variable @@ ERROR. There is no global variable @@ ERROR_MESSAGE to get a description of the error. 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 placeholders (for example,% s,% ld), you can also use this Stored procedure .

SQL Server - .

Bye.

+4

All Articles