Export (0) Print
Expand All

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.

For a list of all members of this type, see SqlDataAdapter Members.

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

[Visual Basic]
NotInheritable Public Class SqlDataAdapter
   Inherits DbDataAdapter
   Implements IDbDataAdapter
[C#]
public sealed class SqlDataAdapter : DbDataAdapter, IDbDataAdapter
[C++]
public __gc __sealed class SqlDataAdapter : public DbDataAdapter,
   IDbDataAdapter
[JScript]
public class SqlDataAdapter extends DbDataAdapter implements
   IDbDataAdapter

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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.

When the SqlDataAdapter fills a DataSet, it will create the necessary tables and columns for the returned data if they do not already exist. However, primary key information will not be 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 Microsoft SQL Server database.

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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Function SelectSqlSrvRows(dataSet As DataSet, connection As String, query As String) As DataSet
    Dim conn As New SqlConnection(connection)
    Dim adapter As New SqlDataAdapter()
    adapter.SelectCommand = new SqlCommand(query, conn)
    adapter.Fill(dataset)
    Return dataset
End Function

[C#] 
public DataSet SelectSqlSrvRows(DataSet dataset,string connection,string query) 
{
    SqlConnection conn = new SqlConnection(connection);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    adapter.Fill(dataset);
    return dataset;
}


[C++] 
public:
DataSet* SelectSqlSrvRows(DataSet* dataset,String* connection,String* query) 
{
    SqlConnection* conn = new SqlConnection(connection);
    SqlDataAdapter* adapter = new SqlDataAdapter();
    adapter->SelectCommand = new SqlCommand(query, conn);
    adapter->Fill(dataset);
    return dataset;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data.SqlClient

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

SqlDataAdapter Members | System.Data.SqlClient Namespace | SqlConnection | SqlCommand | DataSet | DataTable

Show:
© 2014 Microsoft