如何在VB.Net或ASP.Net中创建Windows应用程序

时间:2021-03-30 03:33:41

which method is more efficient to use: DataAdapter and DataSet Or DataReader when importing data from a certain database table???

使用哪种方法更有效:从某个数据库表导入数据时DataAdapter和DataSet或DataReader ???

6 个解决方案

#1


I would just pick whichever one you like best, and decide to optimize once you find a bottleneck in using one or the other. If you're at the place of just planning a new app, it makes more sense to get it working with Okay performance, than to get bogged down trying to pick the 'best' approach.

我会选择你最喜欢的那个,并且一旦找到使用其中一个的瓶颈就决定进行优化。如果您只是计划一个新的应用程序,那么让它与Okay性能一起工作更有意义,而不是陷入困境,试图选择“最佳”方法。

#2


Under the hood, the DataAdapter uses the DataReader to actually query the data and populate the datatable/dataset. So end of the day, it doesn't really matter which one you use, but I would suggest the DataAdapter as it's easier to code against, and less error prone.

在引擎盖下,DataAdapter使用DataReader实际查询数据并填充数据表/数据集。所以当天结束时,使用哪一个并不重要,但我建议使用DataAdapter,因为它更易于编码,并且不易出错。

#3


A Data reader will read the data in a progressive manner allowing you to deal with one row at a time, its fast and light weight but is mainly forward only.

数据读取器将以渐进的方式读取数据,允许您一次处理一行,其快速和轻量级但主要仅向前。

A Data adapter and Dataset, acctually use the DataReader in order to get the data out and stores the result in memory filling the table takes longer but accessing and updating after this is much quicker, until you persist the data back to the DB.

数据适配器和数据集实际上使用DataReader来获取数据并将结果存储在内存中,填充表需要更长的时间,但在此之后访问和更新要快得多,直到您将数据保留回数据库。

Use a datareader if speed is your thing, otherwise use a dataset since its far more readable and managable.

如果速度是你的话,请使用datareader,否则使用数据集,因为它更具可读性和可管理性。

#4


Use case is important here. How are you going to use the data once it's marshaled into your application?

用例在这里很重要。一旦将数据整合到您的应用程序中,您将如何使用这些数据?

If you need only iterate through it, a DataReader has the least memory overhead of your listed technologies.

如果只需要遍历它,DataReader的列出技术的内存开销最小。

DataAdapter provides more capabilities in exchange for more memory, and is specificaly going to involve the overhead of DataTable. This is in exchange for a more convenient API.

DataAdapter提供了更多功能以换取更多内存,并且具体涉及DataTable的开销。这是为了换取更方便的API。

DataSet is heaviest (before you get to typed datasets, at least), but provides the abilitiy to get in-memory relationships between DataRows.

DataSet是最重的(至少在你输入数据集之前),但它提供了在DataRows之间获得内存中关系的能力。

There is a range of capabilities versus cost in terms of memory and programming efficiency - memory is relatively cheap in many use cases, and programmer time and maintainability of code may be more important in the long run.

在内存和编程效率方面,存在一系列功能与成本 - 在许多用例中,内存相对便宜,从长远来看,编程时间和代码的可维护性可能更为重要。

#5


A DataReader is more 'efficient' than a DataAdapter but it has limitations. It's probably the best for your problem, but without further details it is hard to say.

DataReader比DataAdapter更“高效”,但它有局限性。这可能是你问题的最佳选择,但没有进一步的细节,很难说。

And as John says, get a working solution might be your first priority.

正如约翰所说,获得可行的解决方案可能是您的首要任务。

#6


Since it sounds like you're looking for program efficiency. I would lean toward a business object class using a data reader as the access method. That would give you the ability to create maintainable code and give you speed.

因为听起来你正在寻找程序效率。我倾向于使用数据读取器作为访问方法的业务对象类。这将使您能够创建可维护的代码并为您提供速度。

You could create an object with a method that iterates through a DataReader like the example below. This gives you all the speed and in my opinion it's quite maintainable. This is probably pretty much what a type DataSet provides for you, just without more lines of code to generalize the functionality needed in retrieving data.

您可以使用迭代DataReader的方法创建一个对象,如下例所示。这给了你所有的速度,在我看来它是相当可维护的。这可能是DataSet为您提供的类型,只是没有更多行代码来概括检索数据所需的功能。

something like this:

像这样的东西:

public class Table1
{
    public int Col1 { get; set; }
    public String Col2 { get; set; }

    public List<Table1> GetTable1()
    {
        List<Table1> tableContents = new List<Table1>();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Table1");
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            tableContents.Add(new Table1
            {
                Col1 = (int)rdr["Col1"],
                Col2 = (string)rdr["Col2"]
            });
        }

        return tableContents;
    }
}

There's lots of ways to do it, hope this helps

有很多方法可以做到这一点,希望这会有所帮助

#1


I would just pick whichever one you like best, and decide to optimize once you find a bottleneck in using one or the other. If you're at the place of just planning a new app, it makes more sense to get it working with Okay performance, than to get bogged down trying to pick the 'best' approach.

我会选择你最喜欢的那个,并且一旦找到使用其中一个的瓶颈就决定进行优化。如果您只是计划一个新的应用程序,那么让它与Okay性能一起工作更有意义,而不是陷入困境,试图选择“最佳”方法。

#2


Under the hood, the DataAdapter uses the DataReader to actually query the data and populate the datatable/dataset. So end of the day, it doesn't really matter which one you use, but I would suggest the DataAdapter as it's easier to code against, and less error prone.

在引擎盖下,DataAdapter使用DataReader实际查询数据并填充数据表/数据集。所以当天结束时,使用哪一个并不重要,但我建议使用DataAdapter,因为它更易于编码,并且不易出错。

#3


A Data reader will read the data in a progressive manner allowing you to deal with one row at a time, its fast and light weight but is mainly forward only.

数据读取器将以渐进的方式读取数据,允许您一次处理一行,其快速和轻量级但主要仅向前。

A Data adapter and Dataset, acctually use the DataReader in order to get the data out and stores the result in memory filling the table takes longer but accessing and updating after this is much quicker, until you persist the data back to the DB.

数据适配器和数据集实际上使用DataReader来获取数据并将结果存储在内存中,填充表需要更长的时间,但在此之后访问和更新要快得多,直到您将数据保留回数据库。

Use a datareader if speed is your thing, otherwise use a dataset since its far more readable and managable.

如果速度是你的话,请使用datareader,否则使用数据集,因为它更具可读性和可管理性。

#4


Use case is important here. How are you going to use the data once it's marshaled into your application?

用例在这里很重要。一旦将数据整合到您的应用程序中,您将如何使用这些数据?

If you need only iterate through it, a DataReader has the least memory overhead of your listed technologies.

如果只需要遍历它,DataReader的列出技术的内存开销最小。

DataAdapter provides more capabilities in exchange for more memory, and is specificaly going to involve the overhead of DataTable. This is in exchange for a more convenient API.

DataAdapter提供了更多功能以换取更多内存,并且具体涉及DataTable的开销。这是为了换取更方便的API。

DataSet is heaviest (before you get to typed datasets, at least), but provides the abilitiy to get in-memory relationships between DataRows.

DataSet是最重的(至少在你输入数据集之前),但它提供了在DataRows之间获得内存中关系的能力。

There is a range of capabilities versus cost in terms of memory and programming efficiency - memory is relatively cheap in many use cases, and programmer time and maintainability of code may be more important in the long run.

在内存和编程效率方面,存在一系列功能与成本 - 在许多用例中,内存相对便宜,从长远来看,编程时间和代码的可维护性可能更为重要。

#5


A DataReader is more 'efficient' than a DataAdapter but it has limitations. It's probably the best for your problem, but without further details it is hard to say.

DataReader比DataAdapter更“高效”,但它有局限性。这可能是你问题的最佳选择,但没有进一步的细节,很难说。

And as John says, get a working solution might be your first priority.

正如约翰所说,获得可行的解决方案可能是您的首要任务。

#6


Since it sounds like you're looking for program efficiency. I would lean toward a business object class using a data reader as the access method. That would give you the ability to create maintainable code and give you speed.

因为听起来你正在寻找程序效率。我倾向于使用数据读取器作为访问方法的业务对象类。这将使您能够创建可维护的代码并为您提供速度。

You could create an object with a method that iterates through a DataReader like the example below. This gives you all the speed and in my opinion it's quite maintainable. This is probably pretty much what a type DataSet provides for you, just without more lines of code to generalize the functionality needed in retrieving data.

您可以使用迭代DataReader的方法创建一个对象,如下例所示。这给了你所有的速度,在我看来它是相当可维护的。这可能是DataSet为您提供的类型,只是没有更多行代码来概括检索数据所需的功能。

something like this:

像这样的东西:

public class Table1
{
    public int Col1 { get; set; }
    public String Col2 { get; set; }

    public List<Table1> GetTable1()
    {
        List<Table1> tableContents = new List<Table1>();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Table1");
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            tableContents.Add(new Table1
            {
                Col1 = (int)rdr["Col1"],
                Col2 = (string)rdr["Col2"]
            });
        }

        return tableContents;
    }
}

There's lots of ways to do it, hope this helps

有很多方法可以做到这一点,希望这会有所帮助