如何使用 ADO.NET 创建数据源而不是读取和操作数据源

发布于 2024-08-02 14:39:51 字数 959 浏览 2 评论 0 原文

我正在为另一个程序编写一个插件,该程序生成相当复杂的对象树结构。用户需要从插件导出这些数据以进行分析和报告。我绝对希望他们能够导出到 MS Access 数据库,因为这对他们来说是创建快速、干净的报告的最方便的方式。但我还想进行一些设置,以便可以轻松导出到其他数据源(XLS、XML、SQL Server 等),而无需大量重复代码。

我创建了递归方法,将从树结构中填充各种数据表。然后我可以将这些DataTables 填充到DataSet 中。但那时我有点迷失了。

我找到的所有 ADO.NET 示例都是从拥有一些中央数据源开始的。然后,您可以使用连接字符串通过正确的 DataProvider 创建到数据的 DataConnection。之后,您可以获取DataReaderDataSet,具体取决于您是否需要将更改保存回源。

但我从 DataSet 开始,需要从中创建一个数据源。是否有一些简单的可重用方法可以让我基于某些现有的空数据源创建新的DataConnection,然后用我的DataSet 填充它?

例如,我可以创建一个到空 MS Access 文件的DataConnection。然后,我可以使用递归方法从树结构中填充 DataSet 中的各种 DataTables。但是我如何使用生成的DataSet 填充空白访问数据库呢?

我希望有一种足够通用的方法,以便我可以通过简单地交换不同的 DataProviders 和/或连接字符串来轻松导出到不同的潜在数据源。

I'm writing a plug-in for another program that generates a fairly complex tree structure of objects. The users will need to export this data from the plug-in for analysis and reporting. I definitely want them to be able to export to an MS Access database as it would be the most accessible for them to create quick and clean reports. But I also would like to set things up so it would be easy to export to other data sources (XLS, XML, SQL Server etc..) with out a lot of duplicate code.

I have created recursive methods that will populate various DataTables from the tree structure. I can then stuff these DataTables into a DataSet. But at that point I'm kinda lost.

All the examples I find for ADO.NET start with having some central data source. Then you create a DataConnection to the data through the correct DataProvider using a connection string. After which you can get out either a DataReader or DataSet depending on whether you need to save changed back to the source.

Yet I'm starting with the DataSet, and need to create a data source from it. Is there some easy reusable way that I could create a new DataConnection based on some existing empty data source, and then populate it with my DataSet?

For example I could create a DataConnection to an empty MS Access file. Then I could use my recursive methods to populate various DataTables in a DataSet from my tree structure. But how could I populate the blank access database with this resulting DataSet?

I'm hoping for a method that is generic enough so that I can easily export to different potential data sources by simply swapping in different DataProviders and/or connection strings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2024-08-09 14:39:51

您实质上在这里问两个单独的问题,一个是关于在不首先从目标表中进行选择的情况下插入数据,另一个是关于保持持久性代码与数据库无关。

在内存中手动构建数据表并在 DataAdapterDbCommandBuilder。数据适配器用于将数据表同步到数据库中,数据适配器使用命令生成器根据提供的 select 命令自动生成插入(和更新)语句。

只要您不需要运行任何复杂的 SQL 查询,就应该很容易使用 DbProviderFactory。它实质上向您隐藏了 ADO.net 提供程序实现,以便您以通用方式针对底层接口进行编码。

下面的例子应该可以说明以上两个概念。

public static void Main()
{
    var providerName = "System.Data.OleDb";
    var connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                            "Data Source=output.mdb";
    var table = new DataTable {
                        Columns = {
                            new DataColumn("ID", typeof (int)),
                            new DataColumn("Name", typeof (string)) },
                        Rows = {
                            new object[] {1, "One"},
                            new object[] {2, "Two"} }
                    };
    SaveData(providerName, connectionString, table);
}
private static void SaveData(string providerName,
                             string connectionString,
                             DataTable table)
{
    var factory = DbProviderFactories.GetFactory(providerName);
    var connection = factory.CreateConnection();
    connection.ConnectionString = connectionString;
    var command= factory.CreateCommand();
    command.Connection = connection;
    command.CommandText = "select ID, Name from Person";
    var adapter = factory.CreateDataAdapter();
    adapter.SelectCommand = command;
    var builder = factory.CreateCommandBuilder();
    builder.DataAdapter = adapter;
    adapter.Update(table);
}

该解决方案仅处理数据库的持久性。要导出到 Excel,您可以使用 Jet OleDb 提供程序(更多信息此处),对于 XML,您可以可以使用 XmlSerializer

You're essentially asking two separate questions here, one about inserting data without first selecting from the target table and another about keeping your persistence code database agnostic.

It's relatively easy to manually build up a data table in memory and persist it with the help of a DataAdapter and a DbCommandBuilder. The data adapter is used to synchronize the data table into the database and the command builder is used by the data adapter to automatically generate insert (and update) statements based on the select command provided.

So long as you don't need to run any complex SQL queries it should be fairly easy to keep your code database agnostic using a DbProviderFactory. It essentially hides the ADO.net provider implementation from you so that you code against the underlying interfaces in a generic way.

The following example should illustrate the above two concepts.

public static void Main()
{
    var providerName = "System.Data.OleDb";
    var connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                            "Data Source=output.mdb";
    var table = new DataTable {
                        Columns = {
                            new DataColumn("ID", typeof (int)),
                            new DataColumn("Name", typeof (string)) },
                        Rows = {
                            new object[] {1, "One"},
                            new object[] {2, "Two"} }
                    };
    SaveData(providerName, connectionString, table);
}
private static void SaveData(string providerName,
                             string connectionString,
                             DataTable table)
{
    var factory = DbProviderFactories.GetFactory(providerName);
    var connection = factory.CreateConnection();
    connection.ConnectionString = connectionString;
    var command= factory.CreateCommand();
    command.Connection = connection;
    command.CommandText = "select ID, Name from Person";
    var adapter = factory.CreateDataAdapter();
    adapter.SelectCommand = command;
    var builder = factory.CreateCommandBuilder();
    builder.DataAdapter = adapter;
    adapter.Update(table);
}

This solution only deals with persistence to databases. For exporting to Excel you can use the Jet OleDb provider (more info here) and for XML you can use XmlSerializer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文