SqlDataAdapter Class

Definition

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.

public ref class SqlDataAdapter sealed : System::Data::Common::DbDataAdapter, ICloneable, System::Data::IDataAdapter, System::Data::IDbDataAdapter
public ref class SqlDataAdapter sealed : System::Data::Common::DbDataAdapter, ICloneable
public ref class SqlDataAdapter sealed : System::Data::Common::DbDataAdapter
public sealed class SqlDataAdapter : System.Data.Common.DbDataAdapter, ICloneable, System.Data.IDataAdapter, System.Data.IDbDataAdapter
public sealed class SqlDataAdapter : System.Data.Common.DbDataAdapter, ICloneable
public sealed class SqlDataAdapter : System.Data.Common.DbDataAdapter
type SqlDataAdapter = class
    inherit DbDataAdapter
    interface IDbDataAdapter
    interface IDataAdapter
    interface ICloneable
type SqlDataAdapter = class
    inherit DbDataAdapter
    interface IDataAdapter
    interface IDbDataAdapter
    interface ICloneable
Public NotInheritable Class SqlDataAdapter
Inherits DbDataAdapter
Implements ICloneable, IDataAdapter, IDbDataAdapter
Public NotInheritable Class SqlDataAdapter
Inherits DbDataAdapter
Implements ICloneable
Public NotInheritable Class SqlDataAdapter
Inherits DbDataAdapter
Inheritance
SqlDataAdapter
Implements

Examples

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.

using Microsoft.Data.SqlClient;
using System.Xml;
using System.Data.Common;
using System.Windows.Forms;

public class Form1 : Form
{
    protected DataSet DataSet1;
    protected DataGrid dataGrid1;

    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;
        }
    }
}

Remarks

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.

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

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

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 CLR User-Defined Types.

Constructors

SqlDataAdapter()

Initializes a new instance of the SqlDataAdapter class.

SqlDataAdapter(SqlCommand)

Initializes a new instance of the SqlDataAdapter class with the specified SqlCommand as the SelectCommand property.

SqlDataAdapter(String, SqlConnection)

Initializes a new instance of the SqlDataAdapter class with a SelectCommand and a SqlConnection object.

SqlDataAdapter(String, String)

Initializes a new instance of the SqlDataAdapter class with a SelectCommand and a connection string.

Properties

DeleteCommand

Gets or sets a Transact-SQL statement or stored procedure to delete records from the data set.

InsertCommand

Gets or sets a Transact-SQL statement or stored procedure to insert new records into the data source.

SelectCommand

Gets or sets a Transact-SQL statement or stored procedure used to select records in the data source.

UpdateBatchSize

Gets or sets the number of rows that are processed in each round-trip to the server.

UpdateCommand

Gets or sets a Transact-SQL statement or stored procedure used to update records in the data source.

Events

RowUpdated

Occurs during Update(DataSet) after a command is executed against the data source. The attempt to update is made, so the event fires.

RowUpdating

Occurs during Update(DataSet) before a command is executed against the data source. The attempt to update is made, so the event fires.

Explicit Interface Implementations

ICloneable.Clone()

For a description of this member, see Clone().

IDbDataAdapter.DeleteCommand

For a description of this member, see DeleteCommand.

IDbDataAdapter.InsertCommand

For a description of this member, see InsertCommand.

IDbDataAdapter.SelectCommand

For a description of this member, see SelectCommand.

IDbDataAdapter.UpdateCommand

For a description of this member, see UpdateCommand.

Applies to