asp.net.ph

IDataAdapter.Fill Method

System.Data Namespace   IDataAdapter Class


Adds or refreshes rows in the specified DataSet to match those in the data source.

[ VB ]
Function Fill ( _
   ByVal dataSet As DataSet _
) As Integer

[ C# ] int Fill (
   DataSet dataSet
);

[ C++ ] int Fill (
   DataSet* dataSet
 ) = 0;

[ JScript ]
function Fill (
   dataSet : DataSet
) : int;

Parameters

dataSet
A DataSet to fill with records and, if necessary, schema.

Return Value

An integer specifying the number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows.

Remarks

The Fill method retrieves rows from the data source using the SELECT statement specified by the SelectCommand associated with the IDataAdapter. The IDbConnection associated with the SelectCommand must be valid, but it does not need to be open. If the IDbConnection is closed before Fill is called, it is opened to retrieve data, and then closed. If the connection is open before Fill is called, it remains open.

The Fill operation then adds the rows to destination DataTable objects in the DataSet, creating the DataTable objects if they do not already exist. When creating DataTable objects, the Fill operation normally initializes only column name metadata. However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.

If the data adapter encounters duplicate columns while populating a DataTable, it generates names for the subsequent columns, using the pattern "columnname1", "columnname2", "columnname3", and so on. If the incoming data contains unnamed columns, they are placed in the DataSet according to the pattern "Column1", "Column2", and so on.

When multiple result sets are added to the DataSet each result set is placed in a separate table. Additional result sets are named by appending integral values to the specified table name ( for example, "Table", "Table1", "Table2", and so on. ). Applications should use caution when using column and table names to ensure that no naming conflict occurs.

You can use the Fill method multiple times on the same DataTable. If a primary key exists, incoming rows are merged with matching rows that already exist. If no primary key exists, incoming rows are appended to the DataTable.

Example

The following example shows using a derived class, 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

IDataAdapter Members   FillSchema 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