如何在 Linq to SQL 中确定记录已成功添加?
我刚刚学习Linq to SQL,了解不多。请指导和帮助我。
我正在使用 SubmitChanges() 添加一条新记录;如何确认记录已添加?例如,在使用存储过程时,我们将一个标志发送回应用程序,但在 LINQ to SQL 中是如何完成的呢?请指导我。
I am just learning Linq to SQL and don't know much. Kindly guide and help me.
I am adding a new record by using a SubmitChanges(); How I can be confirmed that record has been added? For example, in using stored procedures we use to send a flag back to application but how is it done in LINQ to SQL? Please guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让您的代码流出您的方法。只有当抛出异常时,你的语句才算完成。
如果您想要一个“标志”,您可以返回一个布尔值。
Allow your code to flow out of your method. Only when an exception is throw has your statement not completed.
If you wanted a 'flag', you could return a bool.
你可以做这样的事情;
如果没有抛出异常,您可以假设数据保存正确。虽然另一种选择是这样的。
保存成功则返回true,否则返回false
You could do something like this;
If no exception is thrown you can assume the data is saved correct. Though another option is something like this.
True will be returned if save succeeds, otherwise false will be returned
请参阅:MSDN,如何:将更改提交到数据库< /a>
请注意,存在 的重载SubmitChanges() 可以让您指定如何处理冲突。
See: MSDN, How to: Submit Changes to the Database
Note that there is an overload of SubmitChanges() that will let you specidy how to handle conflicts.
检查此 http://msdn.microsoft.com/en-us/library/bb399378 .aspx
此时,数据库检测到的任何错误都会导致提交过程停止,并引发异常。对数据库的所有更改都会回滚,就像从未发生过提交一样。 DataContext 仍然完整记录了所有更改。因此,您可以尝试纠正问题并再次调用 SubmitChanges
Check this http://msdn.microsoft.com/en-us/library/bb399378.aspx
At this point, any errors detected by the database cause the submission process to stop, and an exception is raised. All changes to the database are rolled back as if no submissions ever occurred. The DataContext still has a full recording of all changes. You can therefore try to correct the problem and call SubmitChanges again