7.3. The System.Data NamespaceSystem.Data and its nested namespaces, notably System.Data.OleDb, System.Data.SqlClient, and System.Data.OracleClient, implement the primary database interaction feature of the .NET Framework, ADO.NET. The OleDb, SqlClient, and OracleClient namespaces define data providers that connect to a data source, retrieve data from a data source, write data back to a data source, and execute commands against the data source. The most important class in each of these namespaces is the data adapter class (in the OleDb namespace, it's the OleDbDataAdapter class; in the SqlClient namespace, it's the SqlDataAdapter class; OracleDataAdapter is its name in the OracleClient namespace), which is used to retrieve data from a data source and write it to a dataset. Datasets in ADO.NET include tables, fields, and their interrelations. They are never directly connected to the original data source; datasets are disconnected. Any data added to them from a database comes through the connected data adapter.
A typical ADO.NET activity involves the retrieval of data from a database, storing the returned records in a dataset. The following function returns a dataset with a single named data table object, based on the records returned from a SQL statement. This example uses the OleDB-focused classes, although the SQL Server or Oracle classes would work the same way. Public Function CreateDataSet(ByVal sqlText As String, _ ByVal tableName As String) As Data.DataSet ' ----- Create a data set/data table from a SQL statement. ' The sqlText argument is the actual SQL statement ' used to retrieve the records. The tableName argument ' gives a meaningful name to the new data set, since ' the data set will not extract it from the SQL code. Dim dbCommand As OleDb.OleDbCommand Dim dbAdaptor As OleDb.OleDbDataAdapter Dim dbNewSet As Data.DataSet dbCommand = New OleDb.OleDbCommand(sqlText, DBLibrary) dbAdaptor = New OleDb.OleDbDataAdapter(dbCommand) dbNewSet = New DataSet dbAdaptor.Fill(dbNewSet, tableName) dbAdaptor = Nothing dbCommand = Nothing Return dbNewSet End Function ADO.NET is a robust and feature-rich set of database interfaces. Due to its size and vast number of options, a full discussion is beyond the scope of this book. For a complete treatment, see Bill Hamilton and Matthew MacDonald's book, ADO.NET in a Nutshell (O'Reilly Media). |