5 out of 6 rated this helpful - Rate this topic

SqlDataAdapter Class

Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database. This class cannot be inherited.

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Data.Common.DataAdapter
        System.Data.Common.DbDataAdapter
          System.Data.SqlClient.SqlDataAdapter

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)
public sealed class SqlDataAdapter : DbDataAdapter, 
	IDbDataAdapter, IDataAdapter, ICloneable

The SqlDataAdapter type exposes the following members.

  Name Description
Public method SqlDataAdapter() Initializes a new instance of the SqlDataAdapter class.
Public method SqlDataAdapter(SqlCommand) Initializes a new instance of the SqlDataAdapter class with the specified SqlCommand as the SelectCommand property.
Public method SqlDataAdapter(String, SqlConnection) Initializes a new instance of the SqlDataAdapter class with a SelectCommand and a SqlConnection object.
Public method SqlDataAdapter(String, String) Initializes a new instance of the SqlDataAdapter class with a SelectCommand and a connection string.
Top
  Name Description
Public property AcceptChangesDuringFill Gets or sets a value indicating whether AcceptChanges is called on a DataRow after it is added to the DataTable during any of the Fill operations. (Inherited from DataAdapter.)
Public property AcceptChangesDuringUpdate Gets or sets whether AcceptChanges is called during a Update. (Inherited from DataAdapter.)
Protected property CanRaiseEvents Gets a value indicating whether the component can raise an event. (Inherited from Component.)
Public property Container Gets the IContainer that contains the Component. (Inherited from Component.)
Public property ContinueUpdateOnError Gets or sets a value that specifies whether to generate an exception when an error is encountered during a row update. (Inherited from DataAdapter.)
Public property DeleteCommand Gets or sets a Transact-SQL statement or stored procedure to delete records from the data set.
Protected property DesignMode Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Protected property Events Gets the list of event handlers that are attached to this Component. (Inherited from Component.)
Protected property FillCommandBehavior Gets or sets the behavior of the command used to fill the data adapter. (Inherited from DbDataAdapter.)
Public property FillLoadOption Gets or sets the LoadOption that determines how the adapter fills the DataTable from the DbDataReader. (Inherited from DataAdapter.)
Public property InsertCommand Gets or sets a Transact-SQL statement or stored procedure to insert new records into the data source.
Public property MissingMappingAction Determines the action to take when incoming data does not have a matching table or column. (Inherited from DataAdapter.)
Public property MissingSchemaAction Determines the action to take when existing DataSet schema does not match incoming data. (Inherited from DataAdapter.)
Public property ReturnProviderSpecificTypes Gets or sets whether the Fill method should return provider-specific values or common CLS-compliant values. (Inherited from DataAdapter.)
Public property SelectCommand Gets or sets a Transact-SQL statement or stored procedure used to select records in the data source.
Public property Site Gets or sets the ISite of the Component. (Inherited from Component.)
Public property TableMappings Gets a collection that provides the master mapping between a source table and a DataTable. (Inherited from DataAdapter.)
Public property UpdateBatchSize Gets or sets the number of rows that are processed in each round-trip to the server. (Overrides DbDataAdapter.UpdateBatchSize.)
Public property UpdateCommand Gets or sets a Transact-SQL statement or stored procedure used to update records in the data source.
Top
  Name Description
Protected method AddToBatch Adds a IDbCommand to the current batch. (Inherited from DbDataAdapter.)
Protected method ClearBatch Removes all IDbCommand objects from the batch. (Inherited from DbDataAdapter.)
Protected method CloneInternals Obsolete. Creates a copy of this instance of DataAdapter. (Inherited from DataAdapter.)
Public method CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Protected method CreateRowUpdatedEvent Initializes a new instance of the RowUpdatedEventArgs class. (Inherited from DbDataAdapter.)
Protected method CreateRowUpdatingEvent Initializes a new instance of the RowUpdatingEventArgs class. (Inherited from DbDataAdapter.)
Protected method CreateTableMappings Creates a new DataTableMappingCollection. (Inherited from DataAdapter.)
Public method Dispose() Releases all resources used by the Component. (Inherited from Component.)
Protected method Dispose(Boolean) Releases the unmanaged resources used by the DbDataAdapter and optionally releases the managed resources. (Inherited from DbDataAdapter.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method ExecuteBatch Executes the current batch. (Inherited from DbDataAdapter.)
Public method Fill(DataSet) Adds or refreshes rows in the DataSet. (Inherited from DbDataAdapter.)
Public method Fill(DataTable) Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataTable name. (Inherited from DbDataAdapter.)
Public method Fill(DataSet, String) Adds or refreshes rows in the DataSet to match those in the data source using the DataSet and DataTable names. (Inherited from DbDataAdapter.)
Protected method Fill(DataTable, IDataReader) Adds or refreshes rows in the DataTable to match those in the data source using the DataTable name and the specified IDataReader. (Inherited from DataAdapter.)
Protected method Fill(DataTable, IDbCommand, CommandBehavior) Adds or refreshes rows in a DataTable to match those in the data source using the specified DataTable, IDbCommand and CommandBehavior. (Inherited from DbDataAdapter.)
Public method Fill(Int32, Int32, DataTable[]) Adds or refreshes rows in a DataTable to match those in the data source starting at the specified record and retrieving up to the specified maximum number of records. (Inherited from DbDataAdapter.)
Public method Fill(DataSet, Int32, Int32, String) Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names. (Inherited from DbDataAdapter.)
Protected method Fill(DataTable[], IDataReader, Int32, Int32) Adds or refreshes rows in a specified range in the collection of DataTable objects to match those in the data source. (Inherited from DataAdapter.)
Protected method Fill(DataSet, String, IDataReader, Int32, Int32) Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names. (Inherited from DataAdapter.)
Protected method Fill(DataTable[], Int32, Int32, IDbCommand, CommandBehavior) Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names. (Inherited from DbDataAdapter.)
Protected method Fill(DataSet, Int32, Int32, String, IDbCommand, CommandBehavior) Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and source table names, command string, and command behavior. (Inherited from DbDataAdapter.)
Public method FillSchema(DataSet, SchemaType) Adds a DataTable named "Table" to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType. (Inherited from DbDataAdapter.)
Public method FillSchema(DataTable, SchemaType) Configures the schema of the specified DataTable based on the specified SchemaType. (Inherited from DbDataAdapter.)
Public method FillSchema(DataSet, SchemaType, String) Adds a DataTable to the specified DataSet and configures the schema to match that in the data source based upon the specified SchemaType and DataTable. (Inherited from DbDataAdapter.)
Protected method FillSchema(DataTable, SchemaType, IDataReader) Adds a DataTable to the specified DataSet. (Inherited from DataAdapter.)
Protected method FillSchema(DataSet, SchemaType, String, IDataReader) Adds a DataTable to the specified DataSet. (Inherited from DataAdapter.)
Protected method FillSchema(DataTable, SchemaType, IDbCommand, CommandBehavior) Configures the schema of the specified DataTable based on the specified SchemaType, command string, and CommandBehavior values. (Inherited from DbDataAdapter.)
Protected method FillSchema(DataSet, SchemaType, IDbCommand, String, CommandBehavior) Adds a DataTable to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType. (Inherited from DbDataAdapter.)
Protected method Finalize Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Protected method GetBatchedParameter Returns a IDataParameter from one of the commands in the current batch. (Inherited from DbDataAdapter.)
Protected method GetBatchedRecordsAffected Returns information about an individual update attempt within a larger batched update. (Inherited from DbDataAdapter.)
Public method GetFillParameters Gets the parameters set by the user when executing an SQL SELECT statement. (Inherited from DbDataAdapter.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method GetService Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method HasTableMappings Indicates whether a DataTableMappingCollection has been created. (Inherited from DataAdapter.)
Protected method InitializeBatching Initializes batching for the DbDataAdapter. (Inherited from DbDataAdapter.)
Public method InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected method OnFillError Invoked when an error occurs during a Fill. (Inherited from DataAdapter.)
Protected method OnRowUpdated Raises the RowUpdated event of a .NET Framework data provider. (Inherited from DbDataAdapter.)
Protected method OnRowUpdating Raises the RowUpdating event of a .NET Framework data provider. (Inherited from DbDataAdapter.)
Public method ResetFillLoadOption Resets FillLoadOption to its default state and causes DataAdapter.Fill to honor AcceptChangesDuringFill. (Inherited from DataAdapter.)
Public method ShouldSerializeAcceptChangesDuringFill Determines whether the AcceptChangesDuringFill property should be persisted. (Inherited from DataAdapter.)
Public method ShouldSerializeFillLoadOption Determines whether the FillLoadOption property should be persisted. (Inherited from DataAdapter.)
Protected method ShouldSerializeTableMappings Determines whether one or more DataTableMapping objects exist and they should be persisted. (Inherited from DataAdapter.)
Protected method TerminateBatching Ends batching for the DbDataAdapter. (Inherited from DbDataAdapter.)
Public method ToString Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)
Public method Update(DataRow[]) Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified array of DataRow objects. (Inherited from DbDataAdapter.)
Public method Update(DataSet) Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet. (Inherited from DbDataAdapter.)
Public method Update(DataTable) Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataTable. (Inherited from DbDataAdapter.)
Protected method Update(DataRow[], DataTableMapping) Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified array of DataRow objects. (Inherited from DbDataAdapter.)
Public method Update(DataSet, String) Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the DataSet with the specified DataTable name. (Inherited from DbDataAdapter.)
Top
  Name Description
Public event Disposed Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Public event FillError Returned when an error occurs during a fill operation. (Inherited from DataAdapter.)
Public event RowUpdated Occurs during Update after a command is executed against the data source. The attempt to update is made, so the event fires.
Public event RowUpdating Occurs during Update before a command is executed against the data source. The attempt to update is made, so the event fires.
Top
  Name Description
Explicit interface implemetation Private method ICloneable.Clone For a description of this member, see Clone.
Explicit interface implemetation Private property IDataAdapter.TableMappings Indicates how a source table is mapped to a dataset table. (Inherited from DataAdapter.)
Explicit interface implemetation Private property IDbDataAdapter.DeleteCommand For a description of this member, see DeleteCommand.
Explicit interface implemetation Private property IDbDataAdapter.InsertCommand For a description of this member, see InsertCommand.
Explicit interface implemetation Private property IDbDataAdapter.SelectCommand For a description of this member, see SelectCommand.
Explicit interface implemetation Private property IDbDataAdapter.UpdateCommand For a description of this member, see UpdateCommand.
Top

The SqlDataAdapter, serves as a bridge between a DataSet and SQL Server for retrieving and saving data. 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, using the appropriate Transact-SQL statements against the data source. The update is performed on a by-row basis. For every inserted, modified, and deleted row, the Update method determines the type of change that has been performed on it (Insert, Update, or Delete). Depending on the type of change, the Insert, Update, or Delete command template executes to propagate the modified row to the data source. When the SqlDataAdapter fills a DataSet, it creates the necessary tables and columns for the returned data if they do not already exist. However, primary key information is not included in the implicitly created schema unless the MissingSchemaAction property is set to AddWithKey. You may also have the SqlDataAdapter create the schema of the DataSet, including primary key information, before filling it with data using FillSchema. For more information, see Adding Existing Constraints to a DataSet (ADO.NET).

SqlDataAdapter is used in conjunction with SqlConnection and SqlCommand to increase performance when connecting to a SQL Server database.

Note Note

If you are using SQL Server stored procedures to edit or delete data using a DataAdapter, make sure that you do not use SET NOCOUNT ON in the stored procedure definition. This causes the rows affected count returned to be zero, which the DataAdapter interprets as a concurrency conflict. In this event, a DBConcurrencyException will be thrown.

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

When an instance of SqlDataAdapter is created, the read/write properties are set to initial values. For a list of these values, see the SqlDataAdapter constructor.

The InsertCommand, DeleteCommand, and UpdateCommand are generic templates that are automatically filled with individual values from every modified row through the parameters mechanism.

For every column that you propagate to the data source on Update, a parameter should be added to the InsertCommand, UpdateCommand, or DeleteCommand. The SourceColumn property of the DbParameter object should be set to the name of the column. This setting indicates that the value of the parameter is not set manually, but is taken from the particular column in the currently processed row.

Note Note

An InvalidOperationException will occur if the Fill method is called and the table contains a user-defined type that is not available on the client computer. For more information, see Creating and Using User-Defined Types in SQL Server Books Online.

The following example uses the SqlCommand, SqlDataAdapter, and SqlConnection to select records from a database and populate a DataSet with the selected rows. The filled DataSet is then returned. To accomplish this, the method is passed an initialized DataSet, a connection string, and a query string that is a Transact-SQL SELECT statement.


private static DataSet SelectRows(DataSet dataset,
    string connectionString,string queryString) 
{
    using (SqlConnection connection = 
        new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(
            queryString, connection);
        adapter.Fill(dataset);
        return dataset;
    }
}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
I wonder ...
What is the difference between these two coding :
new SqlDataAdapter(query, DBAccess.Connect()).Fill(dt);

and
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, DBAccess.Connect()))
{sqlDataAdapter.Fill(dt);}

aren't those two are same? and what do you call the first one?


[tfl - 22 01 12] Hi - and thanks for your post. Community content is not the appropriate place for technical support queries. Instead,
you should visit the Technet Forums at http://forums.microsoft.com/technet, where such posts are welcomed and where you stand a much
better chance of getting your query resolved. Sorry if that's not the answer you wanted to hear.

May I know this ...

Regarding the example code given of selectRows Function, Why is it like that passing the dataset to function byval and then it again returns the same filled dataset.

If I look at use of this official example on this page, it would be something similar to this:

Dim dataSet As New DataSet()
dataSet = selectRows(dataSet, . . . .)

Does above code of lines makes sense ? OR really there is another use of returning the dataset. And What would happen in case of above lines of code. dataSet will be filled within the function and will then again be re written again when the function returns.

And does Reference Values sent to a procedure or function has to be ByVal ?

Please throw some light on this, Thanks.

Regards,
ZH

Esther Fan, MSFT: Thank you for your feedback. For these kinds of questions, please try the MSDN forum: http://forums.microsoft.com/msdn