Internal Design
The Data Access Application Block contains the complete source code and a comprehensive guide to its design. This section describes the main implementation details.
Sqlhelper class Implementation Details
SqlhelperClass is used to encapsulate the data access function through a set of static methods. This class cannot be inherited or instantiated, so it is declared as a non-inherited class that contains a dedicated constructor.
InSqlhelperEach method implemented in the class provides a set of consistent overloading. This provides a good useSqlhelperClass to execute commands, while providing developers with the necessary flexibility to select the way to access data. Each method overload supports different method parameters, so developers can determine how to pass connection, transaction, and parameter information. InSqlhelperThe methods implemented in the class include:
•
Executenonquery. This method is used to execute commands that do not return any rows or values. These commands are usually used to execute database updates, but can also be used to return output parameters of stored procedures.
•
Executereader. This method is used to returnSqldatareaderObject that contains the result set returned by a command.
•
Executedataset. This method returnsDatasetObject that contains the result set returned by a command.
•
Executescalar. This method returns a value. This value is always the first column in the first line returned by the command.
•
Executexmlreader. This method returnsFor XMLThe XML fragment to query.
•
Filldataset. This method is similarExecutedatasetThe difference is that pre-existingDatasetTo allow additional tables to be added.
•
Updatedataset. This method uses existing connections and user-specified update commands to updateDataset. It usually correspondsCreatecommandCommand.
•
Createcommand. This method allows you to provide stored procedures and optional parameters to simplify the creation process of SQL command objects. This method usually worksUpdatedatasetIn combination.
•
Executenonquerytypedparams. This method uses data rows instead of parameters to perform non-query operations.
•
Executedatasettypedparams. This method uses data rows instead of parameters for execution.DatasetCreate operation.
•
Executereadertypedparams. This method uses data rows instead of parameters to return data readers.
•
Executescalartypedparams. This method uses data rows instead of parameters to return scalar values.
•
Executexmlreadertypedparams. This method uses data rows instead of parameters for execution.Xmlreader.
In addition to these public methods,SqlhelperClass also contains some special functions for managing parameters and preparing commands to be executed. No matter what method is called by the client, all commands are implemented by usingSqlcommandObject. Run thisSqlcommandAll parameters must be added toParametersAnd must be set accordingly.Connection,Commandtype,CommandtextAndTransactionAttribute.SqlhelperThe special functions in the class are mainly used to provide a consistent way to execute commands on the SQL Server database without considering the implementation of the overload methods called by client applications.SqlhelperThe special utility functions in the class include:
•
Attachparameters. This function is usedSqlparameterThe object is appended toSqlcommand.
•
Assignparametervalues. This function is usedSqlparameterObject assignment.
•
Preparecommand. This function is used to initialize command attributes (such as connection and transaction context.
•
Executereader.ExecutereaderIs used to openSqldatareaderObject, and use the correspondingCommandbehaviorManage the lifecycle of connections associated with the reader in the most effective way.
Sqlhelperparametercache class Implementation Details
Parameter arrays are cached in dedicatedHashtable. Parameters retrieved from the cache are copied internally, so that the client application can change the parameter value and perform other operations without affecting the cached parameter array. Dedicated shared FunctionsCloneparametersFor this purpose.
Http://www.microsoft.com/china/MSDN/library/EnterpriseDevelopment/BuildDistApp/Vsdnbdadaab_rm.mspx? MFR = true