asp.net.ph

SqlDataAdapter Class

System.Data.SqlClient Namespace


Represents a set of data commands and a database connection which are used to fill the DataSet and update an SQL Server™ database.

SqlDataAdapter Class Members

Collapse   Constructors

Visibility Constructor Parameters
public SqlDataAdapter ( )
public SqlDataAdapter ( SqlCommand selectCommand )
public SqlDataAdapter ( String selectCommandText , String selectConnectionString )
public SqlDataAdapter ( String selectCommandText , SqlConnection selectConnection )

Collapse   Properties

Visibility Name Value Type Accessibility
public DeleteCommand SqlCommand [ Get , Set ]
public InsertCommand SqlCommand [ Get , Set ]
public SelectCommand SqlCommand [ Get , Set ]
public UpdateBatchSize Int32 [ Get , Set ]
public UpdateCommand SqlCommand [ Get , Set ]

Collapse   Methods

Visibility Name Parameters Return Type
protected AddToBatch ( IDbCommand command ) Int32
protected ClearBatch ( ) Void
protected CreateRowUpdatedEvent ( DataRow dataRow , IDbCommand command , StatementType statementType , DataTableMapping tableMapping ) RowUpdatedEventArgs
protected CreateRowUpdatingEvent ( DataRow dataRow , IDbCommand command , StatementType statementType , DataTableMapping tableMapping ) RowUpdatingEventArgs
protected ExecuteBatch ( ) Int32
protected GetBatchedParameter ( Int32 commandIdentifier , Int32 parameterIndex ) IDataParameter
protected GetBatchedRecordsAffected ( Int32 commandIdentifier , Int32& recordsAffected , Exception& error ) Boolean
protected InitializeBatching ( ) Void
protected OnRowUpdated ( RowUpdatedEventArgs value ) Void
protected OnRowUpdating ( RowUpdatingEventArgs value ) Void
protected TerminateBatching ( ) Void

Collapse   Events

Remarks

The SqlDataAdapter serves as a bridge between SQL Server™ and a DataSet, providing the interface for retrieving and saving the data. Using the appropriate Transact-SQL statements against the data source, the SqlDataAdapter provides this bridge by mapping:

  • Fill, which changes the data in the DataSet to match the data in the data source, and
  • Update, which changes the data in the data source to match the data in the DataSet,

SqlDataAdapter is used in conjunction with SqlConnection and SqlCommand to increase performance when connecting to a Microsoft SQL Server™ database. To access other data sources, use OleDbDataAdapter along with its associated OleDbConnection and OleDbCommand.

The SqlDataAdapter also includes the SelectCommand, InsertCommand, DeleteCommand, UpdateCommand, and TableMappings properties for facilitating the loading and updating of data.

A new instance of SqlDataAdapter can be constructed in four ways. For a list of the different versions and the parameters that can be used with each, see the SqlDataAdapter constructor.

Example

The following example shows using an SqlDataAdapter to fill a DataSet with records retrieved from a data source. Here, the entire logic is encapsulated within a Page_Load event handler that essentially:

  1. establishes a connection to the database,
  2. initializes an instance of a DataSet to contain the database information,
  3. and then enumerates the contents of the DataSet into an html table.

string html;

protected void Page_Load ( Object Src, EventArgs e ) {
   // set up the connection
   SqlConnection myConn = new SqlConnection (
      "server=localhost; trusted_connection=yes; database=northwind" );
   // set up the query
   SqlDataAdapter myAdapter = new SqlDataAdapter ( "SELECT * FROM Customers", myConn );

   // instantiate dataset object
   DataSet myData = new DataSet ( );
   // fill with query results
   myAdapter.Fill ( myData, "Customers" );

   // display dataset contents into html table
   // first open the table and set up the table headers
   html += "<table cellspacing=1 class='data' width=80%>";
   html += "<tr>";
   html += "<th>Customer ID</th>";
   html += "<th>Company Name</th>";
   html += "</tr>";

   // loop thru the dataset
   foreach ( DataRow Customer in myData.Tables [ "Customers" ].Rows ) {   
      html += "<tr>";
      html += "<td>" + Customer [ "CustomerID" ].ToString ( ) + "</td>";
      html += "<td>" + Customer [ "CompanyName" ].ToString ( ) + "</td>";
      html += "</tr>";
   }

   // close the table
   html += "</table>";
}
  C# VB

 Show me 

See Also

SqlConnection   SqlCommand   DataSet   DataTable Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph