ADO Error exception handling?

大兔子大兔子 提交于 2019-12-24 02:37:14

问题


I just switched from using the BDE to ADO by replacing the Tables and Queries to its equivalent in ADO components.

I'm always execute the query inside try...catch like this:

//Fdm is Data Module
//TEndOfDay is TTable
//QEndOfDay is TQuery

Screen->Cursor = crSQLWait;
Fdm->QEndOfDay->SQL->Add("SELECT * FROM TEndOfDay");
try{
  Fdm->QEndOfDay->ExecSQL();
  Fdm->QEndOfDay->Open();
  Screen->Cursor = crDefault;
}
catch (EDBEngineError &DBEngineError){
  strError = DBEngineError.Message;
  Screen->Cursor = crDefault;
}
catch (EDatabaseError &DatabaseError){
  strError = DatabaseError.Message;
  Screen->Cursor = crDefault;
}
catch(...){
  strError = "Error";
  Screen->Cursor = crDefault;
}          

Since I switched to ADO, does those exceptions (DBEngineError, DatabaseError) are applicable?

I have been edited my post to include Delphi folks, they are responds quickly. No matter if the answer in Delphi code.


回答1:


You should first check for EADOError, which are specific ADO-related exceptions, and then EDatabaseError, which are more general database exceptions. EDBEngineError is an old BDE exception class, and is not applicable any longer if you're not using the BDE.

Screen.Cursor := crSQLWait;
Fdm.QEndOfDay.SQL.Text := 'SELECT * FROM TEndOfDay';
try
  try
    Fdm.QEndOfDay.Open;
  except
    on E: EAdoError do
    begin
      // ADO specific error handling
    end;
    on E: EDatabaseError do
    begin
      // Generic database error handling
    end;
    on E: Exception do
    begin
      // Other exceptions (non-DB related)
    end;
  end;
finally
  // Revert cursor to default always.
  Screen.Cursor := crDefault;
end;


来源:https://stackoverflow.com/questions/21868588/ado-error-exception-handling

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