System.Data Namespace
Represents the base set of properties and methods used to retrieve records from a data source into a DataSet, and to reconcile the changes back into the data source.
Class |
Description |
DataAdapter |
Represents a set of data commands and a database connection that are used to fill the DataSet and update the data source. |
The IDataAdapter interface allows an inheriting class to implement a DataAdapter class, which represents the bridge between a data source and a DataSet.
An application does not create an instance of the IDataAdapter interface directly, but implements an instance of a class that inherits IDbDataAdapter.
Classes that inherit IDataAdapter must implement the inherited members, and typically define additional members to add provider-specific functionality. For example, the IDataAdapter interface defines a Fill method that takes a DataSet as a parameter. In turn, the OleDbDataAdapter class inherits the Fill method, and also defines two additional overloads of the Fill method that take an ADO Recordset object as a parameter.
Notes to Implementers: To promote consistency among .NET data providers, name the inheriting class in the form Prv DataAdapter where Prv is the uniform prefix given to all classes in a specific .NET data provider namespace. For example, Sql is the prefix of the SqlDataAdapter class in the System.Data.SqlClient namespace.
When you inherit from the IDataAdapter interface, you should implement the following constructors:
Item |
Description |
PrvDataAdapter ( ) |
Initializes a new instance of the PrvDataAdapter class. |
PrvDataAdapter ( PrvCommand selectCommand ) |
Initializes a new instance of the PrvDataAdapter class with the specified SQL SELECT statement. |
PrvDataAdapter ( string selectCommandText, string selectConnectionString ) |
Initializes a new instance of the PrvDataAdapter class with an SQL SELECT statement and a connection string. |
PrvDataAdapter ( string selectCommandText, PrvConnection selectConnection ) |
Initializes a new instance of the PrvDataAdapter class with an SQL SELECT statement and a PrvConnection object. |
For more information about implementing .NET data providers, see Implementing a .NET Data Provider. For more information about using the DataAdapter class, see Populating a DataSet from a DataAdapter.
The following example shows a generic method that is used extensively in the demos of this workshop. Essentially, the method selects records from a given data source and returns a filled DataSet.
For re-usability, the method takes as parameters the statement to use in the query and the database from which to retrieve the result set. The database is passed to the connection string, while the query is passed to the IDataAdapter.
This example implements an SqlDataAdapter, but can be adapted to use an OleDbDataAdapter as well, both of which derive from IDataAdapter.
DataSet fetchData ( string query, string db ) {
// connect to data source
SqlConnection myConn = new SqlConnection (
"server=( local )\\NetSDK; trusted_connection=yes; database=" + db );
// initialize dataadapter
SqlDataAdapter myAdapter = new SqlDataAdapter ( query, myConn );
// initialize and fill DataSet
DataSet myDataSet = new DataSet ( );
myAdapter.Fill ( myDataSet );
// return dataset
return myDataSet;
}
sub fetchData ( query as string, db as string ) as DataSet
' connect to data source
dim myConn as new SqlConnection (
"server=( local )\NetSDK; trusted_connection=yes; database=" & db )
' initialize dataadapter
dim myAdapter as new SqlDataAdapter ( query, myConn )
' initialize and fill DataSet
dim myDataSet as new DataSet ( )
myAdapter.Fill ( myDataSet )
' return dataset
return myDataSet
end sub |
|
C# |
VB |
The below examples illustrate using the method above, passing different parameters to query different data sources and return varying result sets.
DataSet SqlDataAdapter OleDbDataAdapter