如何对数据库做出错处理(100分)

  • 主题发起人 主题发起人 anderson_pee
  • 开始时间 开始时间
A

anderson_pee

Unregistered / Unconfirmed
GUEST, unregistred user!
在用C/S做的管理系统中delphi前端SQL后端用ADO连接,在登录时如果用户密码输入错误
SQL就会报错,我想知道如何用Delphi来处理这个错误以便用户及时重输密码而不必重新
登录。
 
利用try
except
就可以实现屏蔽系统提示的错误消息
 
try
输入密码判断等操作
....
except
..if 取消Application.Terminate;
if 重新输入..
end;
 
大家能否说得详细一点,有具体步骤,我对这方面毫无经验,如果我满意还可以加分
 
请问try
DataBase1.Connect;
except
on E : *** do begin
end;
end;
中的***是什么?


 
想法:
如果仅仅是用户与密码的问题,在没有增加或修改的前提下,
可以先把用户与密码取出,也就是说不管他输入错误多少次,
对数据库的连接仅为一次,而且加上if,else并没有发现系统
报错。
 
及时发现,那就输入时就判断一下.
 
我想说的他们都说了。
 
如何使用try...except还请各位大虾帮帮忙,给小弟详细的指点?
 
Exceptions are handled within try...except statements. For example,

try

X := Y/Z;
except
on EZeroDivide do HandleZeroDivide;//这里具体到了EZeroDivide这个错误,写,表示所有的错误
end;

This statement attempts to divide Y by Z, but calls a routine named HandleZeroDivide if an EZeroDivide exception is raised.
The syntax of a try...except statement is

try statements except exceptionBlock end

where statements is a sequence of statements (delimited by semicolons) and exceptionBlock is either

another sequence of statements or
a sequence of exception handlers, optionally followed by

else statements

An exception handler has the form

on identifier: type do statement

where identifier: is optional (if included, identifier can be any valid identifier), type is a type used to represent exceptions, and
statement is any statement.
A try...except statement executes the statements in the initial
statements list. If no exceptions are raised, the exception block (
exceptionBlock) is ignored and control passes to the next part of the program.
If an exception is raised during execution of the initial statements
list, either by a raise statement in the statements list or by a procedure or function called from the statements list, an attempt is made to 揾andle?the exception:

If any of the handlers in the exception block matches the exception, control passes to the first such handler. An exception handler 搈atches?an exception just in case the
type in the handler is the class of the exception or an ancestor of that class.
If no such handler is found, control passes to the statement in the else clause, if there is one.
If the exception block is just a sequence of statements without any exception handlers, control passes to the first statement in the list.

If none of the conditions above is satisfied, the search continues in the exception block of the next-most-recently entered try...except statement that has not yet exited. If no appropriate handler, else clause, or statement list is found there, the search propagates to the next-most-recently entered try...except statement, and so forth. If the outermost try...except statement is reached and the exception is still not handled, the program terminates.

When an exception is handled, the stack is traced back to the procedure or function containing the try...except statement where the handling occurs, and control is transferred to the executed exception handler, else clause, or statement list. This process discards all procedure and function calls that occurred after entering the try...except statement where the exception is handled. The exception object is then automatically destroyed through a call to its Destroy destructor and control is passed to the statement following the try...except statement. (If a call to the Exit, Break, or Continue standard procedure causes control to leave the exception handler, the exception object is still automatically destroyed.)

In the example below, the first exception handler handles division-by-zero exceptions, the second one handles overflow exceptions, and the final one handles all other math exceptions.
EMathError appears last in the exception block because it is the ancestor of the other two exception classes; if it appeared first, the other two handlers would never be invoked.

try

...
except
on EZeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
end;

An exception handler can specify an identifier before the name of the exception class. This declares the identifier to represent the exception object during execution of the statement that follows
on...do. The scope of the identifier is limited to that statement. For example,

try

...
except
on E: Exception do ErrorDialog(E.Message, E.HelpContext);
end;

If the exception block specifies an else clause, the else clause handles any exceptions that aren抰 handled by the block抯 exception handlers. For example,

try

...
except
on EZeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
else
HandleAllOthers;
end;

Here, the else clause handles any exception that isn抰 an EMathError.
An exception block that contains no exception handlers, but instead consists only of a list of statements, handles all exceptions. For example,

try

...
except
HandleException;
end;

Here, the HandleException routine handles any exception that occurs as a result of executing the statements between try and
except.
 
这些帮助文件我都看过了,还是不能理解其精髓。
比如说try的后面是写处理错误的语句还是问题发生前的正常语句?
on后面的exception是写什么?是写系统提示给我的exception(如EOleException)吗?
我写了上述exception后Delphi又说是未定义的变量。
我希望能有一个具体的例子(最好和我的问题有关)而帮助中又没有。真急。
 
在窗体上放一tquery,sql string为‘select * from tabelname=:username’,代码如下

procedure TForm1.Button1Click(Sender: TObject);
var sql:string;
begin
query1.Close;
query1.ParamByName('username').asstring:=edit1.text;
query1.open;
if query1.FieldValues['password']=null then
showmessage('用户名错误')
{application.Terminate;}
else
if query1.fieldvalues['password']=edit2.text then
form2.show
else
showmessage('密码错误');
end;
 
登录窗口是ADOConnection自带的,不能为上面的button加代码。
我现在也只对如何用try...exception来处理该问题感兴趣。还望各位大虾包涵!
 
try
ADOConnection.connected := True ;
Except
ShowMessage('登陆失败');
End;
 
完颜兄:
此代码加到程序中的哪个事件里?
 
后退
顶部