SqlDataAdapter Constructors

Definition

Overloads

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.

SqlDataAdapter()

Initializes a new instance of the SqlDataAdapter class.

public:
 SqlDataAdapter();
public SqlDataAdapter ();
Public Sub New ()

Examples

The following example creates a SqlDataAdapter and sets some of its properties.

public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
{
    // Assumes that connection is a valid SqlConnection object
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the commands.
    adapter.SelectCommand = new SqlCommand(
        "SELECT CustomerID, CompanyName FROM CUSTOMERS", connection);
    adapter.InsertCommand = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);
    adapter.UpdateCommand = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);
    adapter.DeleteCommand = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        SqlDbType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        SqlDbType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    return adapter;
}

Remarks

When an instance of SqlDataAdapter is created, the following read/write properties are set to the following initial values.

Properties Initial value
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

You can change the value of any of these properties through a separate call to the property.

Applies to

SqlDataAdapter(SqlCommand)

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

public:
 SqlDataAdapter(Microsoft::Data::SqlClient::SqlCommand ^ selectCommand);
public SqlDataAdapter (Microsoft.Data.SqlClient.SqlCommand selectCommand);
new Microsoft.Data.SqlClient.SqlDataAdapter : Microsoft.Data.SqlClient.SqlCommand -> Microsoft.Data.SqlClient.SqlDataAdapter
Public Sub New (selectCommand As SqlCommand)

Parameters

selectCommand
SqlCommand

A SqlCommand that is a Transact-SQL SELECT statement or stored procedure and is set as the SelectCommand property of the SqlDataAdapter.

Examples

The following example creates a SqlDataAdapter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
    }
    public static SqlDataAdapter CreateSqlDataAdapter(SqlCommand selectCommand,
        SqlConnection connection)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        // Create the other commands.
        adapter.InsertCommand = new SqlCommand(
            "INSERT INTO Customers (CustomerID, CompanyName) " +
            "VALUES (@CustomerID, @CompanyName)", connection);

        adapter.UpdateCommand = new SqlCommand(
            "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
            "WHERE CustomerID = @oldCustomerID", connection);

        adapter.DeleteCommand = new SqlCommand(
            "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

        // Create the parameters.
        adapter.InsertCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID");
        adapter.InsertCommand.Parameters.Add("@CompanyName",
            SqlDbType.VarChar, 40, "CompanyName");

        adapter.UpdateCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID");
        adapter.UpdateCommand.Parameters.Add("@CompanyName",
            SqlDbType.VarChar, 40, "CompanyName");
        adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
            SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

        adapter.DeleteCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

        return adapter;
    }
}

Remarks

This implementation of the SqlDataAdapter constructor sets the SelectCommand property to the value specified in the selectCommand parameter.

When an instance of SqlDataAdapter is created, the following read/write properties are set to the following initial values.

Properties Initial value
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

You can change the value of any of these properties through a separate call to the property.

When SelectCommand (or any of the other command properties) is assigned to a previously created SqlCommand, the SqlCommand is not cloned. The SelectCommand maintains a reference to the previously created SqlCommand object.

Applies to

SqlDataAdapter(String, SqlConnection)

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

public:
 SqlDataAdapter(System::String ^ selectCommandText, Microsoft::Data::SqlClient::SqlConnection ^ selectConnection);
public SqlDataAdapter (string selectCommandText, Microsoft.Data.SqlClient.SqlConnection selectConnection);
new Microsoft.Data.SqlClient.SqlDataAdapter : string * Microsoft.Data.SqlClient.SqlConnection -> Microsoft.Data.SqlClient.SqlDataAdapter
Public Sub New (selectCommandText As String, selectConnection As SqlConnection)

Parameters

selectCommandText
String

A String that is a Transact-SQL SELECT statement or stored procedure to be used by the SelectCommand property of the SqlDataAdapter.

selectConnection
SqlConnection

A SqlConnection that represents the connection. If your connection string does not use Integrated Security = true, you can use SqlCredential to pass the user ID and password more securely than by specifying the user ID and password as text in the connection string.

Examples

The following example creates a SqlDataAdapter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
    }
    public static SqlDataAdapter CreateSqlDataAdapter(string commandText,
        SqlConnection connection)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection);

        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        // Create the other commands.
        adapter.InsertCommand = new SqlCommand(
            "INSERT INTO Customers (CustomerID, CompanyName) " +
            "VALUES (@CustomerID, @CompanyName)");

        adapter.UpdateCommand = new SqlCommand(
            "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
            "WHERE CustomerID = @oldCustomerID");

        adapter.DeleteCommand = new SqlCommand(
            "DELETE FROM Customers WHERE CustomerID = @CustomerID");

        // Create the parameters.
        adapter.InsertCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID");
        adapter.InsertCommand.Parameters.Add("@CompanyName",
            SqlDbType.VarChar, 40, "CompanyName");

        adapter.UpdateCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID");
        adapter.UpdateCommand.Parameters.Add("@CompanyName",
            SqlDbType.VarChar, 40, "CompanyName");
        adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
            SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

        adapter.DeleteCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

        return adapter;
    }
}

Remarks

This implementation of the SqlDataAdapter opens and closes a SqlConnection if it is not already open. This can be useful in an application that must call the Fill method for two or more SqlDataAdapter objects. If the SqlConnection is already open, you must explicitly call Close or Dispose to close it.

When an instance of SqlDataAdapter is created, the following read/write properties are set to the following initial values.

Properties Initial value
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

You can change the value of either of these properties through a separate call to the property.

Applies to

SqlDataAdapter(String, String)

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

public:
 SqlDataAdapter(System::String ^ selectCommandText, System::String ^ selectConnectionString);
public SqlDataAdapter (string selectCommandText, string selectConnectionString);
new Microsoft.Data.SqlClient.SqlDataAdapter : string * string -> Microsoft.Data.SqlClient.SqlDataAdapter
Public Sub New (selectCommandText As String, selectConnectionString As String)

Parameters

selectCommandText
String

A String that is a Transact-SQL SELECT statement or stored procedure to be used by the SelectCommand property of the SqlDataAdapter.

selectConnectionString
String

The connection string. If your connection string does not use Integrated Security = true, you can use SqlDataAdapter(String, SqlConnection) and SqlCredential to pass the user ID and password more securely than by specifying the user ID and password as text in the connection string.

Examples

The following example creates a SqlDataAdapter and sets some of its properties.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
    }
    public static SqlDataAdapter CreateSqlDataAdapter(string commandText,
        string connectionString)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(commandText, connectionString);
        SqlConnection connection = adapter.SelectCommand.Connection;

        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        // Create the commands.
        adapter.InsertCommand = new SqlCommand(
            "INSERT INTO Customers (CustomerID, CompanyName) " +
            "VALUES (@CustomerID, @CompanyName)", connection);

        adapter.UpdateCommand = new SqlCommand(
            "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
            "WHERE CustomerID = @oldCustomerID", connection);

        adapter.DeleteCommand = new SqlCommand(
            "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

        // Create the parameters.
        adapter.InsertCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID");
        adapter.InsertCommand.Parameters.Add("@CompanyName",
            SqlDbType.VarChar, 40, "CompanyName");

        adapter.UpdateCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID");
        adapter.UpdateCommand.Parameters.Add("@CompanyName",
            SqlDbType.VarChar, 40, "CompanyName");
        adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
            SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

        adapter.DeleteCommand.Parameters.Add("@CustomerID",
            SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;

        return adapter;
    }
}

Remarks

This overload of the SqlDataAdapter constructor uses the selectCommandText parameter to set the SelectCommand property. The SqlDataAdapter will create and maintain the connection created with the selectConnectionString parameter.

When an instance of SqlDataAdapter is created, the following read/write properties are set to the following initial values.

Properties Initial value
MissingMappingAction MissingMappingAction.Passthrough
MissingSchemaAction MissingSchemaAction.Add

You can change the value of any of these properties through a separate call to the property.

Applies to