How many did not read the MSDN did not understand why, when filling in the datagridview, also prescribe the SqlDataAdapter and the DataTable is not enough? Maybe the question is stupid, but I want to understand and not just write code that would work.

  • In short, the DataAdapter provides database queries and translates the results into a DataTable. - rdorn
  • but the query can be done without the DataAdapter. for example, add a row to the database or read the contents of a column. - Sergey
  • With DataTable itself, yes, you can do anything with your hands. And if you need the results of a request from the server? there may not be a single table, it may be necessary to send the updated data from the DataTable back, of course you can all by hand, but why? - rdorn
  • one
    and why then DataTable? here we need to fill in the table from the database on the form. What is the logic of this process? SqlConnection - creates a connection, SqlCommand - sends a SQL request over the specified connection, SqlDataReader - reads the received data. and DataAdapter and DataTable that do? - Sergey
  • one
    DataReader and DataAdapter do roughly the same thing, only the reader reads the result line by line, and the adapter is entirely, and DataTable and DataSet are just a wrapper for presenting tabular results of the query to the database. A kind of database cache on the client side. - rdorn

1 answer 1

First, let's turn to the documentation. I cite links to Russian pages, but the translation is automatic there, and I don’t like it, so the quote will be original.

SqlDataReader

Provides a SQL Server database.

SqlDataAdapter

The SQL Server database is a representation of a set of data commands.

In fact, both classes provide access to the database data, but in slightly different ways.

SqlDataReader provides strictly sequential access and reads data in small chunks, which allows you to work even with huge tables without the risk of getting an exception by timeout. But on this his dignity ends, he does not know how anything else.

SqlDataAdapter provides a rich interface for automatically filling the DataTable and the DataSet , and also provides the means of synchronizing data between the DataSet and the database, but at the same time, in practice, it is quite whimsical for too large data sets, because trying to get them all. If the data set exceeds a certain amount (depending on the connection settings, network delays, server load, etc.) then the adapter will not have enough time to receive it and an exception will be thrown.

There is no universal recipe for using here, just like when working with files. Somewhere it is more profitable to use a reader, somewhere an adapter.

DataTable and DataSet , according to the documentation and practical application, represent the database data cache in the client's memory. DataSet consists of a DataTable and can optionally contain details of the relational model of cached data.

DataTable , in particular, can be used as a DataSource for controls like GridView, ListView and others. intended to display arrays of data.