C#中:使用字符串指定的TableAdapter编程方式调用TableAdapters.Fill方法 [英] c#: Programatically call a TableAdapters.Fill Method using a string to Specify the TableAdapter

查看:318
本文介绍了C#中:使用字符串指定的TableAdapter编程方式调用TableAdapters.Fill方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候宗师的

我有一个特殊的问题的TableAdapter和补法。 我有一堆自动生成的TableAdapter的,作为一个应用程序绑定到数据源的结果。 他们必然要对我的表单和工作控件预期。

I have a peculiar problem with TableAdapters and the Fill Method. I have a bunch of autogenerated TableAdapters as a result of binding an app to a DataSource. They are bound to controls on my form and work as expected.

现在我需要调用的TableAdapter的填充方法不时 我想这样做编程(TableAdapter的[名称]填充(myDataSet [Name_Table]);。) 而不是字面(TableAdapter_Name.Fill(myDataSet.Name_Table);

Now I need to call the fill method on the TableAdapters from time to time and I want to do It programatically (TableAdapters["Name"].Fill(myDataSet["Name_Table"]);) instead of literally (TableAdapter_Name.Fill(myDataSet.Name_Table);

我们的想法是一个字符串传递给可通过解析串以识别所需的对象调用目标的TableAdapter的方法。 我常常希望的C#支持创建动态报表/方法(从字符串),可以像一个普通的语句来执行。

The Idea is to pass a string to a method that can call the targeted TableAdapter by parsing the string to recognize the desired object. I've often wished that C# supported creating dynamic statements/methods (from strings) that can be executed like a regular statement.

这可能吗? (如果不是,好像我会做文字输入的很多重present我所有的TableAdapter的,当我做了很多的字符串字面打字在我的code这是一个红色的标志,我米做错了什么

Is this possible? (If not it seems like I'll be doing a LOT of literal typing to represent all of my TableAdapters and when I do a lot of literal typing of strings in my code it's a RED FLAG that I'm doing something wrong"

帮助,锡安

NameTableAdapter.Fill(myDataSet.Name); StateTableAdapter.Fill(myDataSet.State); CityTableAdapter.Fill(myDataSet.City);

NameTableAdapter.Fill(myDataSet.Name); StateTableAdapter.Fill(myDataSet.State); CityTableAdapter.Fill(myDataSet.City);

推荐答案

基本上你问的是这样的JavaScript 的eval(...)语句。不,C#不有,有很好的理由。别的不说,那就要启动一个新的编译器对于这种在运行时每个实例。可能有一些库或外部工具提供这种类型的功能,但我会非常谨慎使用他们这样的事情的。

Essentially you are asking for something like the javascript eval(...) statement. No, C# doesn't have that, and for good reason. Among other things, it would have to fire up a new compiler for every instance of this at runtime. There may be some libraries or external tools that provide this type of functionality, but I would be very wary of using them for something like this.

如果这些TableAdapter的是一种形式,那么他们是实例成员,你可以使用 Type.GetField 的方法来找到适配器,然后 Type.GetMethod 找到在TableAdapter上调用填充方法。

If these TableAdapters are on a form, then they are instance members and you could use the Type.GetField method to find the adapter and then the Type.GetMethod to find the Fill method on the TableAdapter to invoke.

不过,我的问题是,为什么你需要做到这一点?对我来说,红旗的是,有人试图通过数据层来创建这样一个混乱的抽象。需要考虑的事项:

But my question is, why do you need to do this? To me, the red flag is the fact that somebody is trying to create such a messy abstraction over a data layer. Things to consider:

  • 在大多数数据库有小到足以倾倒所有行从没有杀害数据库和应用程序的性能非常少的表。您应该几乎从来不使用该参数的填写 / 的GetData 的方法。
  • 在使用反射意味着你没有得到任何编译时检查,除非它是非常严格的控制(即由于一些DI框架的一部分),它可能导致神秘的,难以调试的错误的道路。
  • 填写方法本身需要一个类型化的参数,所以如果你试图写一个通用补法,它使得人写code这传递了不正确的表型,这可能导致各种有趣的和未predictable问题。
  • 在一般的TableAdapter的 - 怎么说呢这一点 - 不完全是德precated,但不是preferred的数据接入手段的今天。实体框架和LINQ to SQL是更具吸引力的选择。除非你已经从previous设计师继承了这个DAL,为什么要使用它呢?
  • Most databases have very few tables that are small enough to dump all rows from without killing both database and app performance. You should almost never use the parameterless Fill/GetData methods.
  • Using Reflection means you don't get any compile-time checking, and unless it's very tightly controlled (i.e. as part of some DI framework), it's likely to lead to mysterious, hard-to-debug errors down the road.
  • The Fill method itself takes a typed parameter, so if you try to write a "generic" fill method, it makes it possible for someone to write code that passes an incorrect table type, which could lead to all sorts of fun and unpredictable problems.
  • TableAdapters in general are - how should I say this - not exactly deprecated, but not the preferred means of data access today. Entity Framework and Linq to SQL are much more attractive options. Unless you've inherited this DAL from a previous designer, why use it at all?

我想你应该解释一下你的问题域。我敢打赌,有一个更好的设计,可为您的特殊要求。

I think you should explain more about your problem domain. I am willing to bet that there is a better design available for your particular requirements.

这篇关于C#中:使用字符串指定的TableAdapter编程方式调用TableAdapters.Fill方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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