다음을 통해 공유


SqlCeDataAdapter.UpdateCommand 속성

데이터 소스에서 레코드를 업데이트하는 데 사용되는 SQL 문을 가져오거나 설정합니다.

네임스페이스: System.Data.SqlServerCe
어셈블리: System.Data.SqlServerCe(system.data.sqlserverce.dll)

구문

‘선언
Public Property UpdateCommand As SqlCeCommand
‘사용 방법
Dim instance As SqlCeDataAdapter
Dim value As SqlCeCommand

value = instance.UpdateCommand

instance.UpdateCommand = value
public SqlCeCommand UpdateCommand { get; set; }
public:
property SqlCeCommand^ UpdateCommand {
    SqlCeCommand^ get ();
    void set (SqlCeCommand^ value);
}
/** @property */
public SqlCeCommand get_UpdateCommand ()

/** @property */
public void set_UpdateCommand (SqlCeCommand value)
public function get UpdateCommand () : SqlCeCommand

public function set UpdateCommand (value : SqlCeCommand)

속성 값

DataSet의 수정된 행에 해당하는 데이터 소스의 레코드를 업데이트하는 Update 동안 사용된 SqlCeCommand입니다.

설명

Update를 호출하는 동안, 이 속성이 설정되어 있지 않고 기본 키 정보가 DataSet에 있으면 UpdateCommand는 자동으로 생성될 수 있습니다. 이 속성이 자동 생성되도록 하려면 SelectCommand 속성을 설정하고 SqlCeCommandBuilder를 사용합니다. 그러면 설정하지 않은 모든 추가 명령이 SqlCeCommandBuilder에서 생성됩니다. 이러한 생성 논리를 적용하려면 키 열에 대한 정보가 DataSet에 있어야 합니다.

UpdateCommand를 이전에 만든 SqlCeCommand에 할당하면 SqlCeCommand가 복제되지 않습니다. UpdateCommand는 이전에 만든 SqlCeCommand 개체 참조를 유지합니다.

참고

이 명령의 실행으로 행이 반환되면 해당 행은 SqlCeCommand 개체의 UpdatedRowSource 속성을 설정하는 방법에 따라 DataSet에 병합될 수 있습니다.

예제

다음 예제에서는 SqlCeDataAdapter를 만들고 데이터 집합을 수정한 다음 Update 메서드를 호출하여 데이터 소스를 업데이트합니다.

Dim cmd As SqlCeCommand = Nothing
Dim adp As SqlCeDataAdapter = Nothing

Try
    adp = New SqlCeDataAdapter()
    Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf")

    ' Create the SelectCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "SELECT [Employee ID], [First Name], [Last Name] FROM Employees"
    adp.SelectCommand = cmd

    ' Create the InsertCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "INSERT INTO Employees ([First Name],[Last Name]) VALUES (@first, @last)"

    Dim p As SqlCeParameter = Nothing

    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name")
    p.SourceVersion = DataRowVersion.Original

    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name")
    p.SourceVersion = DataRowVersion.Original

    adp.InsertCommand = cmd

    ' Create the UpdateCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "UPDATE Employees SET [First Name] = @first, " + _
        "[Last Name] = @last WHERE [Employee ID] = @employeeID"

    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name")
    p.SourceVersion = DataRowVersion.Current

    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name")
    p.SourceVersion = DataRowVersion.Current

    p = cmd.Parameters.Add("@employeeID", SqlDbType.NVarChar, 20, "Employee ID")
    p.SourceVersion = DataRowVersion.Original

    adp.UpdateCommand = cmd

    ' Populate the dataset with the results from the SELECT statement
    '
    Dim ds As New DataSet()
    adp.Fill(ds)

    ' Modify the dataset
    '
    MessageBox.Show("Number of rows: " & ds.Tables(0).Rows.Count)

    ' Insert some rows
    '
    ds.Tables(0).Rows.Add(New Object() {Nothing, "Nancy", "Smith"})
    ds.Tables(0).Rows.Add(New Object() {Nothing, "Joe", "Clayton"})

    ' Update some rows
    '
    ds.Tables(0).Rows(1)(1) = "David"
    ds.Tables(0).Rows(1)(2) = "Johnson"

    ' This will execute two INSERT and one UPDATE statements
    '
    adp.Update(ds.Tables(0))
Catch e As Exception
    MessageBox.Show(e.Message)
Finally
    If Not Nothing Is adp.SelectCommand Then
        adp.SelectCommand.Dispose()
    End If
    If Not Nothing Is adp.InsertCommand Then
        adp.InsertCommand.Dispose()
    End If
End Try
SqlCeCommand cmd = null;
SqlCeDataAdapter adp = null;

try
{
    adp = new SqlCeDataAdapter();
    SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");

    // Create the SelectCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT [Employee ID], [First Name], [Last Name] FROM Employees";
    adp.SelectCommand = cmd;

    // Create the InsertCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "INSERT INTO Employees ([First Name],[Last Name]) VALUES (@first, @last)";

    SqlCeParameter p = null;

    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name");
    p.SourceVersion = DataRowVersion.Original;

    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name");
    p.SourceVersion = DataRowVersion.Original;

    adp.InsertCommand = cmd;

    // Create the UpdateCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "UPDATE Employees SET [First Name] = @first, " +
        "[Last Name] = @last WHERE [Employee ID] = @employeeID";

    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name");
    p.SourceVersion = DataRowVersion.Current;

    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name");
    p.SourceVersion = DataRowVersion.Current;

    p = cmd.Parameters.Add("@employeeID", SqlDbType.NVarChar, 20, "Employee ID");
    p.SourceVersion = DataRowVersion.Original;

    adp.UpdateCommand = cmd;

    // Populate the dataset with the results from the SELECT statement
    //
    DataSet ds = new DataSet();
    adp.Fill(ds);

    // Modify the dataset
    //
    MessageBox.Show("Number of rows: " + ds.Tables[0].Rows.Count);

    // Insert some rows
    //
    ds.Tables[0].Rows.Add(new object[] { null, "Nancy", "Smith" });
    ds.Tables[0].Rows.Add(new object[] { null, "Joe", "Clayton" });

    // Update some rows
    //
    ds.Tables[0].Rows[1][1] = "David";
    ds.Tables[0].Rows[1][2] = "Johnson";

    // This will execute two INSERT and one UPDATE statements 
    //
    adp.Update(ds.Tables[0]);

    
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}
finally
{
    if (null != adp.SelectCommand) adp.SelectCommand.Dispose();
    if (null != adp.InsertCommand) adp.InsertCommand.Dispose();
}

.NET Framework 보안

  • 직접 실행 호출자의 경우 완전히 신뢰합니다. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다. 자세한 내용은 을(를) 참조하십시오.

플랫폼

Windows CE, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows XP Professional x64 Edition, Windows XP SP2

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

SqlCeDataAdapter 클래스
SqlCeDataAdapter 멤버
System.Data.SqlServerCe 네임스페이스
DeleteCommand
InsertCommand
SelectCommand