我想在数据库中插入数据时如何在应用程序本身中获取消息 [英] how can i get a message in the application itself when i want to insert data into the database

查看:74
本文介绍了我想在数据库中插入数据时如何在应用程序本身中获取消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,


其实我已经在表上写了一个触发器,当我们想向该表中插入数据时,触发器会根据给定的条件触发.这是我要做的一点是我必须显示消息(触发器给出的内容)在应用程序(正在运行)的消息框中.





谢谢您,

Sir,


Actually i have written a trigger on a table.when we want to insert data into that table the trigger fires based on the condition given.here is the point what i want to do is i have to display the message( what the trigger gives) in a message box in the application(which is running).





Thank you,

推荐答案

在我看来,这似乎不是一个正确的设计.您可以修改插入存储过程,并从那里返回消息.或者,您的触发器必须正在将该消息写入审核表或类似内容中.因此,一旦插入完成,请阅读审核表并从那里获取消息.

顺便说一句,这个触发器是做什么的?您想显示哪种消息?

更新:错误的设计.对不起.在保存之前,应在UI和BusinessLogic中对此进行检查.此检查也应放在执行插入操作的存储过程中. BusinessLogic和SP都应抛出异常以告知价格不正确.最好自定义某种东西,例如BusinessValidationException.

然后,您的UI应处理此异常并将消息显示给用户.

您不需要,也不能使用触发器(假设该列中未存储超过1000的值).

更新2:您不必等到不正确的数据到达DB.在UI中检查它,在BusinessLayer中检查它,然后在DB中也检查它.这是应该做的. UI可以向用户显示验证消息. BusinessLayer和DB必须告知UI数据错误.这可以通过方法的返回值或通过异常来完成.触发器并非用于此目的.
This doesn''t looks like a correct design to me. You cam modify the insert stored procedure and return the message from there. Or, your trigger must be writing that message in an audit table or something similar. So once the insert is done, read the audit table and get the message from there.

BTW what does this trigger do? And what kind of message you want to display?

Update: Bad design. Sorry. This should be checked in the UI and in the BusinessLogic before doing the save. This check should also be placed in the stored procedure doing the insert. Both BusinessLogic and SP should throw an exception to tell that the price is not correct. Something custom preferably like BusinessValidationException.

Your UI should then handle this exception and show the message to the user.

You do not need and cannot use trigger for that (assuming values over 1000 are not stored in the column).

Update 2: You do not have to wait till incorrect data reaches DB. Check it in the UI, check it in the BusinessLayer and then check it in the DB as well. This is how it should be done. UI can show the validation message to the user. BusinessLayer and DB have to tell the UI that the data is wrong. This can either be done through a return value from the methods or through exceptions. Triggers are not for this purpose.


一种方法是使用单独的表.将消息添加到触发器中的此表中,例如:
One way is that you use a separate table. Add the messages to this table in the trigger, for example:
INSERT INTO Messages (...) VALUES (.., ''Something from the trigger'')

操作完成后,从客户端读取表的内容.

但是,根据您的示例,您不应创建这样的设计来获得客户的确认. ACID的基本规则是与用户的对话是这样的:
1.有效性检查,可能存在的问题
2.开始交易
3.执行dml操作
4.提交/回滚
5.通知用户

因此,在事务处于活动状态时,永远不会与用户进行对话.如果违反了这一黄金法则,您将面临严重的基于锁定的问题.

And after the operation is done, read the contents of the table from the client.

However, based on your example you should not create a design like this to get confirmations from client. The basic rules of ACID is that conversations with the user go like this:
1. Validity checks, possible questions
2. start transaction
3. execute dml operations
4. commit/rollback
5. inform the user

So there''s no conversation, never, with the user while the transaction is active. If this golden rule is broken, you''ll end up to serious locking based problems.


这篇关于我想在数据库中插入数据时如何在应用程序本身中获取消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆