DataAdapter内部用DataReader填充DataTable,为何属于断开式架构?
Great question—this is a super common point of confusion, so let's break it down clearly.
First, let's nail down what disconnected architecture actually means: it's a pattern where your app doesn't keep a persistent, open connection to the database. Instead, you load data into an in-memory structure (like a DataTable), close the database connection, work with the data locally, and only re-establish the connection when you need to sync changes back to the database.
Now, let's unpack how DataAdapter fits into this, even with its internal use of DataReader:
- When you call
DataAdapter.Fill()to populate aDataTable:- The
DataAdapterchecks the state of the underlying database connection. If it's closed, it temporarily opens the connection only for this specific operation. - It uses a
DataReaderunder the hood to stream data from the database efficiently—this is just an implementation detail for fast, low-memory data retrieval. - Once all data is loaded into the
DataTable, theDataAdaptercloses the connection (if it was the one that opened it in the first place). TheDataReaderis then disposed of, since its job is done.
- The
The critical thing here is that the DataReader is just a temporary tool for the DataAdapter. Sure, DataReader itself requires an open connection to work, but that connection is only kept open for the duration of the fill operation. Once the DataTable is populated, you're left with an in-memory dataset that's completely disconnected from the database—you can modify it, query it, or pass it around without any active database connection.
In short: The DataAdapter uses a connected component (DataReader) briefly to fetch data, but the end result is a disconnected DataTable, and no persistent connection is maintained after the operation finishes. That's why it's firmly part of the disconnected architecture pattern.
内容的提问来源于stack exchange,提问作者I Love Stackoverflow

