.NET - 有没有一种方法以编程方式填写一个强类型DataSet中的所有表? [英] .NET - Is there a way to programmatically fill all tables in a strongly-typed dataset?

查看:176
本文介绍了.NET - 有没有一种方法以编程方式填写一个强类型DataSet中的所有表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我创建了一个强类型DataSet(使用Visual Studio 2008 DataSet设计器)SQL Server数据库中,因此所有的适配器,并选择命令和诸如此类的东西向导为我创建的。

I have a SQL Server database for which I have created a strongly-typed DataSet (using the DataSet Designer in Visual Studio 2008), so all the adapters and select commands and whatnot were created for me by the wizard.

这是一个小数据库,主要是静态数据,所以我想这个数据库的全部内容拉进我的启动应用程序,然后根据使用LINQ需要抓住各个部分的数据。而不是C每个适配器填充调用硬$ C $,我想看看是否有一种方法来自动完成这个(通过反射可能)。

It's a small database with largely static data, so I would like to pull the contents of this DB in its entirety into my application at startup, and then grab individual pieces of data as needed using LINQ. Rather than hard-code each adapter Fill call, I would like to see if there is a way to automate this (possibly via Reflection).

所以,相反的:

Dim _ds As New dsTest
dsTestTableAdapters.Table1TableAdapter.Fill(_ds.Table1)
dsTestTableAdapters.Table2TableAdapter.Fill(_ds.Table2)
<etc etc etc>

我会preFER做这样的事情:

I would prefer to do something like:

Dim _ds As New dsTest
For Each tableName As String In _ds.Tables
    Dim adapter as Object = <routine to grab adapter associated with the table>
    adapter.Fill(tableName)
Next

时,即使远程可行?我已经做搜索了相当多的,而且我不认为这会是一种不常见的请求,但是我必须是问错了问题,或者说我只是奇怪想做到这一点。

Is that even remotely doable? I have done a fair amount of searching, and I wouldn't think this would be an uncommon request, but I must be either asking the wrong question, or I'm just weird to want to do this.

我承认,我通常preFER使用未绑定的控件和不与强类型数据集去(我preFER直接写SQL),但我的公司希望走这条路,所以我研究它。我认为这个想法是,作为添加表,我们就可以使用Visual Studio的设计师刷新DataSet并没有作太多的底层数据库code的变化。

I will admit that I usually prefer to use unbound controls and not go with strongly-typed datasets (I prefer to write SQL directly), but my company wants to go this route, so I'm researching it. I think the idea is that as tables are added, we can just refresh the DataSet using the Designer in Visual Studio and not have to make too many underlying DB code changes.

任何帮助都将是最AP preciated。在此先感谢!

Any help at all would be most appreciated. Thanks in advance!

推荐答案

还有不存在任何API,可以让你做到这一点自动补全类型化数据集或没有这样的code是类型化数据集内产生支持这一点。这也是很难做到这一点,因为TableAdapter的没有一个共同的基类,可以让你做到这一点。

There does not exists any api that lets you do this auto-fill of the entire typed-dataset or no such code is generated within typed-dataset that supports this. It is also difficult to do this because TableAdapters do not have a common base-class that can let you do this.

如果你真的需要做到这一点,你必须保持数据表类型名称和TableAdapter的类型名称的集合和遍历集合来执行数据集填充。

If you really need to do this, you'll have to maintain a collection of DataTable type-names and TableAdapter type-names and iterate over the collection to perform the dataset fill.

因此​​,我建议,以填补数据集的硬code'的方式,你的第一个code实例状态的每个表。

So I recommend to fill dataset for each table in 'hard-code' manner as your first code examples states.

下面是一个可能的解决方案。

Here's one possible solution.

定义一个接口ITableAdapter如下

Define an Interface ITableAdapter as following

public interface ITableAdapter<TDataTable> : where TDataTable : DataTable
{
    TDataTable SelectAll();
}

所有的TableAdapter是部分类,这样可以延长它们并添加自定义code局部自定义类的TableAdapter。实施ITableAdapter在你的类型化数据集的TableAdapter的每个。所以它可能是这样的。

All TableAdapters are partial classes, so you can extend them and add your custom code in partial custom class for TableAdapter. Implement ITableAdapter on each TableAdapter in your typed-data-set. so it might look like this.

public partial class YourTableAdapter : ITableAdapter<YourDataSet.YourDataTableDataTable>
{
    public YourDataSet.YourDataTableDataTable SelectAll()
    {
         return this.GetData();
    }
}

现在,您可以遍历各类型的组装和过滤那些类型ITableAdapter,并呼吁全选()对他们每个人的方法填充到数据集。 :)

Now, you can iterate over each type in your assembly and filter those of type ITableAdapter and call SelectAll() method on each of them fill it into your Dataset. :)

我刚刚想出了这个问题的另一种优雅的解决方案。所有你需要做的就是定义ITableAdapter映射的数据集设计师产生,在TableAdapter的已实现的方法的接口。

I just came up with another elegant solution for this problem. All you need to do is define the Interface ITableAdapter to map the already implemented methods in TableAdapters that are generated by the dataset-designer.

public interface ITableAdapter<TDataTable> : where TDataTable : DataTable
{
    void Fill(TDataTable);
}

和扩展您的TableAdapter部分类这样的。

And extend your TableAdapter partial classes like this.

public partial class YourTableAdapter : ITableAdapter<YourDataSet.YourDataTableDataTable>
{
    //No code required here, since Fill method is already defined in TableAdapter :)
}

这篇关于.NET - 有没有一种方法以编程方式填写一个强类型DataSet中的所有表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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