Export (0) Print
Expand All

SqlDataAdapter Constructor

.NET Framework 1.1

Initializes a new instance of the SqlDataAdapter class.

Overload List

Initializes a new instance of the SqlDataAdapter class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public SqlDataAdapter();
[C++] public: SqlDataAdapter();
[JScript] public function SqlDataAdapter();

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(SqlCommand)
[C#] public SqlDataAdapter(SqlCommand);
[C++] public: SqlDataAdapter(SqlCommand*);
[JScript] public function SqlDataAdapter(SqlCommand);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, SqlConnection)
[C#] public SqlDataAdapter(string, SqlConnection);
[C++] public: SqlDataAdapter(String*, SqlConnection*);
[JScript] public function SqlDataAdapter(String, SqlConnection);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, String)
[C#] public SqlDataAdapter(string, string);
[C++] public: SqlDataAdapter(String*, String*);
[JScript] public function SqlDataAdapter(String, String);

Example

[Visual Basic, C#, C++] The following example creates a SqlDataAdapter and sets some of its properties.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the SqlDataAdapter constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub CreateSqlDataAdapter()
    Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", _
        "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")
    Dim custConn as SqlConnection = custDA.SelectCommand.Connection
    custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey    
   
    custDA.InsertCommand = New SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _
                                            "VALUES (@CustomerID, @CompanyName)", custConn)
    custDA.UpdateCommand = New SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
                                            "WHERE CustomerID = @oldCustomerID", custConn)
    custDA.DeleteCommand = New SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", custConn)

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

    custDA.DeleteCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original
End Sub

[C#] 
public static void CreateSqlDataAdapter() 
{
    SqlDataAdapter custDA = new SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", 
         "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
    SqlConnection custConn = custDA.SelectCommand.Connection;

    custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
   
    custDA.InsertCommand = new SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " +
                                            "VALUES (@CustomerID, @CompanyName)", custConn);
    custDA.UpdateCommand = new SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
                                            "WHERE CustomerID = @oldCustomerID", custConn);
    custDA.DeleteCommand = new SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", custConn);

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

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

[C++] 
public:
static void CreateSqlDataAdapter() 
{
    SqlDataAdapter* custDA = new SqlDataAdapter(S"SELECT CustomerID, CompanyName FROM Customers", 
         S"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
    SqlConnection* custConn = custDA->SelectCommand->Connection;

    custDA->MissingSchemaAction = MissingSchemaAction::AddWithKey;
   
    custDA->InsertCommand = new SqlCommand(S"INSERT INTO Customers (CustomerID, CompanyName) " 
                                           S"VALUES (@CustomerID, @CompanyName)", custConn);
    custDA->UpdateCommand = new SqlCommand(S"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " 
                                           S"WHERE CustomerID = @oldCustomerID", custConn);
    custDA->DeleteCommand = new SqlCommand(S"DELETE FROM Customers WHERE CustomerID = @CustomerID", custConn);

    custDA->InsertCommand->Parameters->Add(S"@CustomerID", SqlDbType::Char, 5, S"CustomerID");
    custDA->InsertCommand->Parameters->Add(S"@CompanyName", SqlDbType::VarChar, 40, S"CompanyName");
 
    custDA->UpdateCommand->Parameters->Add(S"@CustomerID", SqlDbType::Char, 5, S"CustomerID");
    custDA->UpdateCommand->Parameters->Add(S"@CompanyName", SqlDbType::VarChar, 40, S"CompanyName");
    custDA->UpdateCommand->Parameters->Add(S"@oldCustomerID", SqlDbType::Char, 5, S"CustomerID")->SourceVersion = DataRowVersion::Original;

    custDA->DeleteCommand->Parameters->Add(S"@CustomerID", SqlDbType::Char, 5, S"CustomerID")->SourceVersion = DataRowVersion::Original;
}

[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.

See Also

SqlDataAdapter Class | SqlDataAdapter Members | System.Data.SqlClient Namespace

Show:
© 2014 Microsoft