PETSHOP has only one project, which uses the hierarchical method to write the intermediate layer and data layer in the Components directory, where the data layer is a class named Database, which encapsulates all the underlying operations of the database. Below is a sample code segment:
Public void RunProc (String Procname, Out SqlDataReader DataRead) {
Sqlcommand cmd = createcommand (procname, null);
DataReader = cmd.executeRead (System.data.commandbehavior.CloseConnection);
}
We saw another data access method with Duwamish, which abstract all the data access methods to make a RunProc method, as for the return data, huh, it is a bit lazy, return to a DataReader to you, you Go to read yourself. Remember what the interlayer data transmission carrier used by DUWAMISH? Yes, it is DataSet, which is returned to the intermediate layer after being filled by the data layer. However, the data transmission carrier of the data layer and the transport layer becomes DataReader. In fact, it is not called a data carrier, because the data has not started reading, here, DataReader's role and pointer a bit similar, maybe we should Call it for "data reference" :)
Then look down, DataReader is "handled":
Public productResults [] getList (String Catid, Int Currentpage, Int PageSize, Ref Int NumResults)
{
NumResults = 0;
INT INDEX = 0;
SqlDataReader Reader = getList (catid);
ProductResults [] results = new productResults [Pagesize];
// Now loop through the list and pull out items of the specified page
INT START = (INT) (CurrentPage - 1) * pageSize
IF (start <= 0) st = 1;
// Skip
For (int i = 0; i IF (Reader.Read ()) NumResults ; } IF (start> 1) reader.read (); // read the data we are intended in While (Reader.Read ()) { IF (INDEX Results [index] = new productResults (); Results [index] .productId = reader.getstring (0); Results [index] .name = reader.getstring (1); INDEX ; } NumResults ; } Reader.Close (); // See if need need to redim array IF (index == pagesize) Return Results; Else { // Not a full Page, Redim Array ProductResults [] Results2 = new productResults [index]; array.copy; results, result2, index; Return Results2; } } Do you notice CurrentPage and PageSize? It turned out that the data paging was conducted here, only returned to the minimum amount of data that met, not like a lot of people who like to be lazy, simply bind the entire DataTable a brain to the DataGrid, causing a lot of data redundancy. Here, the data is real read, and is manually filled into a custom object array, let's take a look at the definition of this array: Public Class ProductResults PUBLIC CLASS { PRIVATE STRING M_PRODUCTID; Private string m_name; // Product Props Public string productId { Get {return m_productid;} Set {m_ProductId = value;} } PUBLIC STRING NAME { Get {return m_name;} Set {m_name = value;} } } Very simple, but I am a little strange why not using Struct? Is the performance gap between Struct and Class in .NET already ignore it? analysis Summary By observing the specific implementation of these two stores, we have received two different data access modes, and duwamish uses Dataset as the core, because Dataset provides a large number of related methods this, so the data transmission, data format of the entire application Definitions, data checks are carried out around Dataset, and the entire architecture defines very clear and rigorous, but it is a bit large. PETSHOP did not use a DataSet throughout the program, the program is very simple, light, but there is no duwamish so strong. These two programs are code written by Microsoft's different teams, so they have different styles. However, you should represent the standard mode of .NET. Seeing this, you should have a comparative understanding of the questions that start with the beginning of the article. Also, note that PETSHOP is not immediately read after opening the data connection, but passes the DataReader to another object to perform the data read operation, and then turn off the connection. In this way, the data connection is longer, and the database connection is a very valuable server resource. In contrast, Dawam is filling immediately after connecting the database, and then quickly releases the database connection, which is more conducive to a large number of users. access. One point, the update operation is not mentioned above, and PETSHOP uses the COMMAND object to perform a single stored procedure to update the operation, which belongs to an online instant data update mode. Dawamish uses the DataAdapter's UPDATE method, submits the DataSet's change in a one-time submission to the database, which is an offline data update mode. The advantage of this mode is to update large quantities of data at once, reducing the number of connections to the database. The disadvantage is that if the database needs real-time tracking data change in the event of a very frequent change, it is not suitable. Specific data update methods are required to be used depending on the specific situation. In general, if you only need to quickly read the data and display it, it is recommended that you use DataReader if you need to make a lot of modifications to the data, and there is a large number of incoming access to the possibility, and does not require real-time tracking database changes. It is recommended that you use DataSet. Of course, these two cases are very extreme. The actual application environment may have a very complex condition. Specific need for your own review, comprehensive adoption, but I personally still like Petshop's lighter style :) This article only tries to The data access mechanism of two typical .NET application routines made a simple tracking analysis.