SqlDataAdapter.InsertCommand 속성

정의

새로운 레코드를 데이터 소스에 삽입할 Transact-SQL 문이나 저장 프로시저를 가져오거나 설정합니다.

public:
 property Microsoft::Data::SqlClient::SqlCommand ^ InsertCommand { Microsoft::Data::SqlClient::SqlCommand ^ get(); void set(Microsoft::Data::SqlClient::SqlCommand ^ value); };
public Microsoft.Data.SqlClient.SqlCommand InsertCommand { get; set; }
member this.InsertCommand : Microsoft.Data.SqlClient.SqlCommand with get, set
Public Property InsertCommand As SqlCommand

속성 값

SqlCommand의 새 행과 일치하는 데이터베이스에 레코드를 삽입하는 Update(DataSet) 중에 사용되는 DataSet입니다.

예제

다음 예제에서는 를 만들고 SqlDataAdapter , InsertCommand, UpdateCommandDeleteCommand 속성을 설정합니다SelectCommand. 개체를 이미 만들었다고 가정합니다 SqlConnection .

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
    }
    public static SqlDataAdapter CreateCustomerAdapter(
        SqlConnection connection)
    {
        SqlDataAdapter adapter = new SqlDataAdapter();

        // Create the SelectCommand.
        SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
            "WHERE Country = @Country AND City = @City", connection);

        // Add the parameters for the SelectCommand.
        command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
        command.Parameters.Add("@City", SqlDbType.NVarChar, 15);

        adapter.SelectCommand = command;

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

        // Add the parameters for the InsertCommand.
        command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
        command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

        adapter.InsertCommand = command;

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

        // Add the parameters for the UpdateCommand.
        command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
        command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
        SqlParameter parameter = command.Parameters.Add(
            "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
        parameter.SourceVersion = DataRowVersion.Original;

        adapter.UpdateCommand = command;

        // Create the DeleteCommand.
        command = new SqlCommand(
            "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

        // Add the parameters for the DeleteCommand.
        parameter = command.Parameters.Add(
            "@CustomerID", SqlDbType.NChar, 5, "CustomerID");
        parameter.SourceVersion = DataRowVersion.Original;

        adapter.DeleteCommand = command;

        return adapter;
    }
}

설명

중에 Update이 속성이 설정되지 않고 기본 키 정보가 에 DataSetInsertCommand 있는 경우 속성을 설정하고 SelectCommand 를 사용하는 SqlCommandBuilder경우 가 자동으로 생성될 수 있습니다. 그런 다음 설정하지 않은 추가 명령은 에 의해 SqlCommandBuilder생성됩니다. 이 세대 논리에 키 열 정보가 필요 합니다 DataSet합니다. 자세한 내용은 CommandBuilder를 사용하여 명령 생성을 참조하세요.

가 이전에 만든 SqlCommand에 할당된 경우 InsertCommandSqlCommand 복제되지 않습니다. 는 InsertCommand 이전에 만든 SqlCommand 개체에 대한 참조를 유지 관리합니다.

이 명령을 실행하면 행이 반환되면 개체의 UpdatedRowSource 속성을 SqlCommand 설정하는 방법에 따라 이러한 행을 에 추가할 DataSet 수 있습니다.

의 데이터 원본에 전파하는 모든 열에 Update대해 매개 변수를 , UpdateCommand또는 DeleteCommandInsertCommand추가해야 합니다. SourceColumn 매개 변수의 속성을 열 이름으로 설정해야 합니다. 이는 매개 변수의 값이 수동으로 설정되지 않고 현재 처리된 행의 특정 열에서 가져온 것임을 나타냅니다.

적용 대상