在表单上声明变量 TableAdapter
希望这是一个相当简单的问题。
我正在我的应用程序中构建通用搜索表单。这将允许用户在整个应用程序中搜索各种记录。
我似乎无法弄清楚的一件事是如何允许 TableAdapter 的声明在运行时更改。应用程序的每个部分都会向搜索表单传递一个变量,以指定应加载哪个表。
在表单类中,我有以下内容:
FRIEND WITHEVENTS tbaSearchData AS database.databaseTableAdapters.TableOneTableAdapter
这对于 TableOne 来说非常有用。但是,我有大约一百个表可以搜索。
为了加载数据,我使用 DataGridView 并通过私有方法填充它。
任何帮助将不胜感激。
This is hopefully a fairly straightforward question.
I am building a generic search form in my application. This will allow the user to search for various records throughout the application.
The one thing I cannot seem to figure out is how to allow the declaration of the TableAdapter to change at run-time. Each part of the app will be passing a variable to the search form to specify which table should be loaded.
In the form class I have the following:
FRIEND WITHEVENTS tbaSearchData AS database.databaseTableAdapters.TableOneTableAdapter
This is great for TableOne. But, I have about a hundred tables that could be searched through.
To load the data I'm using a DataGridView and populating it via a private method.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Tableadapter 实际上是用于设计时设置的,并且在运行时不容易更改。您可能需要考虑使用 DataAdapter(例如 SqlDataAdapter)来处理这些请求。这些对象的开销较低,并且不像表适配器那样特定于类型,因此在运行时创建它们是一个不错的解决方案。
Tableadapter is really meant for design time setup and doesn't easily change at runtime. You may want to look into using a DataAdapter, like SqlDataAdapter, for these requests. These objects have lower overhead and aren't as type specific as a tableadapter so making them at runtime is a decent solution.
让我们看看我是否理解了这一点。
我提出了两种可能的解决方案
从应用程序的各个角度,您可以传递专门的 DataAdapter 本身而不是表名,并将通用 DataAdapter 变量分配到搜索表单中。
您可以在搜索表单中创建通用 DataAdapter 并更改用于加载数据的 SQL 命令。在下面的代码中,
da
将是一个 DataAdapter,table
是传递到搜索表单的表名da.SelectCommand.CommandText = "Select * from " &表
Let's see if I understood this.
I've come up with two possible solutions
From various point of your app, you could pass the specialized DataAdapter itself instead of a tablename and assign to a generic DataAdapter variable into the search form.
You could create a generic DataAdapter into the search form and change the SQL command used to load the data. In the following code,
da
will be a DataAdapter andtable
the tablename passed to the search formda.SelectCommand.CommandText = "Select * from " & table