다음을 통해 공유


SqlDataAdapter.UpdateCommand 속성

정의

데이터 원본의 레코드를 업데이트하는 데 사용되는 Transact-SQL 문 또는 저장 프로시저를 가져오거나 설정합니다.

public:
 property System::Data::SqlClient::SqlCommand ^ UpdateCommand { System::Data::SqlClient::SqlCommand ^ get(); void set(System::Data::SqlClient::SqlCommand ^ value); };
[System.Data.DataSysDescription("DbDataAdapter_UpdateCommand")]
public System.Data.SqlClient.SqlCommand UpdateCommand { get; set; }
public System.Data.SqlClient.SqlCommand UpdateCommand { get; set; }
[<System.Data.DataSysDescription("DbDataAdapter_UpdateCommand")>]
member this.UpdateCommand : System.Data.SqlClient.SqlCommand with get, set
member this.UpdateCommand : System.Data.SqlClient.SqlCommand with get, set
Public Property UpdateCommand As SqlCommand

속성 값

SqlCommand 에서 수정된 행에 해당하는 데이터베이스의 레코드를 DataSet업데이트하는 동안 Update(DataSet) 사용됩니다.

특성

예제

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

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;
}
Public Function CreateCustomerAdapter( _
  ByVal connection As SqlConnection) As SqlDataAdapter

    Dim adapter As SqlDataAdapter = New SqlDataAdapter()

    ' Create the SelectCommand.
    Dim command As SqlCommand = 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")
    Dim parameter As SqlParameter = 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.
    command.Parameters.Add( _
        "@CustomerID", SqlDbType.NChar, 5, "CustomerID")
    parameter.SourceVersion = DataRowVersion.Original

    adapter.DeleteCommand = command

    Return adapter
End Function

설명

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

이전에 만든 SqlCommandSqlCommand 데이터베이스에 할당된 경우 UpdateCommand 복제되지 않습니다. 이전에 UpdateCommand 만든 SqlCommand 개체에 대한 참조를 유지 관리합니다.

메모

이 명령을 실행하면 행이 반환되면 업데이트된 DataSet 행이 개체의 SqlCommandUpdatedRowSource 속성을 설정하는 방법에 따라 병합될 수 있습니다.

데이터 원본에 전파하는 모든 열에 대해 Update매개 변수를 추가하거나 에 추가InsertCommandUpdateCommandDeleteCommand해야 합니다.

SourceColumn 매개 변수의 속성을 열 이름으로 설정해야 합니다. 이는 매개 변수 값이 수동으로 설정되지 않고 현재 처리된 행의 특정 열에서 가져온 것임을 나타냅니다.

적용 대상

추가 정보