The D layer is responsible for interacting with the database while the machine room is being reconstructed. The D layer queries the database, and the value returned is the DataTable object. A DataTable object can be used in the system to assign values to a TextBox, DataGridView, and other controls on a form, or to determine whether a query exists in the database by the returned DataTable object. In general, the use of a DataTable involving systems interacting with a database is very extensive. This article is mainly a simple introduction to the DataTable.
A DataTable object is a virtual data table in memory that consists primarily of a DataRow object and a DataColumn object. A DataTable object is the primary component that makes up a DataSet object, because the DataSet object can receive data obtained after the SQL instruction executed by the DataAdapter object, which is the format of the DataTable object. So the DataSet object also needs many DataTable objects to store the data, and the Add method in the DataRows collection object can be used to add new data. The DataTable class belongs to the System.Data namespace, so you must reference the System.Data namespace if you want to use a DataTable object. The commonly used properties of a DataTable object are shown in the table:
In the machine room reconstruction through the D Layer Query database Return DataTable object method, using the SqlHelper class, the following for the SqlHelper class in the database query operation code:
'///<summary> '/////To perform a query operation, (with a parameter) The return value is a DataTable type '///</summary> '//<param name= "strsq L "> need to execute statements, typically SQL statements, and stored Procedures </param> '///<param name=" Cmdtype "> Determine the type of SQL statement, generally not a stored procedure </param> '///<param name= ' sqlparams ' > parameter array, unable to confirm how many parameters </param> '///<returns> '///< return table > '/// </returns> public Function ExecuteSelect (ByVal strSQL as String, ByVal Cmdtype as CommandType, ByVal Sqlparams As SqlParameter ()) as DataTable Dim sqladapter As SqlDataAdapter ' define data Table Parameters Dtsql Dim dtsql As New Data Table () ' Defines the DataSet parameter Dssql Dim dssql as New DataSet ' populates the class's own CMD object with the arguments passed in cmdsql.commandtext = St Rsql Cmdsql.commandtype = Cmdtype cmdsql.connection = Cnnsql ' parameter add cmdSQL.Parameters.AddRange ( Sqlparams) ' Instantiation Adapter Sqladapter = New SqlDataAdapter (cmdsql) Try ' using adapter to set the dataset Fill SqladapteR.fill (Dssql) ' DataTable is the first table of the DataSet Dtsql = dssql.tables (0) ' Purge parameters Cmdsql . Parameters.clear () Catch ex as Exception MsgBox ("Query Failed", CType (vbOKOnly + msgboxstyle.exclamation, MsgBox Style), "warning") finally ' be sure to destroy the CMD call Closesqlcommand () End Try ' return value DATATABL E object Return dtsql End Function In the SqlHelper class, when the queried data is populated into a dataset (DataSet), the DataSet is a collection of DataTable, including one or more DataTable. by "dtsql=dssql.tables (0)" This code extracts the first table in the dataset as the return value of the DataTable, and finally gets the table we query.
Summary
The DataTable plays a big role in implementing the binding of controls and data in the form. However, the DataTable in the three layer of data transfer has its shortcomings, the specific content please see the article generic collection instead of a DataTable.
PS: The above is only a personal understanding of the DataTable, if there are shortcomings, please treatise!
First knowledge of a DataTable