如何重写 DataSet 中表的 TableAdapter 方法?
我目前声明了一个 DataSet,其中包含 3 个表。在这个例子中,我们将它们称为“用户”、“问题”和“答案”。
在每一个上,我都有一个 TableAdapter ,其中包含所需的各种方法,即。 GetData()、Update()、Delete() 等。
在答案表上,我想重写 TableAdapter 中的更新方法以添加一些不属于表的参数,但由于 ObjectDataSource 要求,我需要传递。
如何覆盖 Answer TableAdapter 上的 Update() 方法?
为了使项目保持简单,我不想创建单独的 DAL 层。
I currently have a single DataSet declared which contains 3 tables. For sake of this example we will call them User, Question and Answer.
On each of these I have a TableAdapter with the various methods required, ie. GetData(), Update(), Delete() etc.
On the Answer Table I would like to override the Update Method from the TableAdapter to add some Parameters which are not part of the table, but I need to pass due to the ObjectDataSource requirement.
How do I go about overriding the Update() method on the Answer TableAdapter?
In trying to keep the project simple, I don't want to create a separate DAL layer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何设计者生成的
TableAdapter
类都将其适当的成员标记为虚拟 - 这包括Update
和Fill
方法等。因此,解决方案只是继承设计者生成的TableAdapter
类并重写Update
方法,并在其中添加自定义代码。如果您想更改方法签名(参数计数/类型),您还可以选择重载
Update
方法。您可以在派生类上执行此操作,或者在我看来更方便地使用扩展方法:Any designer-generated
TableAdapter
class its appropiate members marked as virtual - this includes theUpdate
andFill
method among others. Hemce, the solution is simply to inherit from the designer-generatedTableAdapter
class and override theUpdate
method, adding your custom code there.You also have the option of overloading the
Update
method, if you want to change the method signature (parameter count/types). You could do that either on a derived class, or more conveniently in my view, using an extension method: