SqlCeDataAdapter.UpdateCommand プロパティ

名前空間:  System.Data.SqlServerCe
アセンブリ:  System.Data.SqlServerCe (System.Data.SqlServerCe.dll)

構文

'宣言
Public Property UpdateCommand As SqlCeCommand
    Get
    Set
'使用
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);
}
member UpdateCommand : SqlCeCommand with get, set
function get UpdateCommand () : SqlCeCommand
function set UpdateCommand (value : SqlCeCommand)

プロパティ値

型: System.Data.SqlServerCe.SqlCeCommand
Update 処理中に、DataSet 内の変更行に対応するデータ ソース内のレコードを更新するための SqlCeCommand

説明

Update の処理中に、このプロパティが設定されておらず、DataSet に主キー情報が存在する場合、UpdateCommand が自動的に生成されます。このプロパティが自動的に生成されるようにするには、SelectCommand プロパティを設定し、SqlCeCommandBuilder を使用します。続いて、設定していない追加のコマンドが、SqlCeCommandBuilder によって生成されます。この生成ロジックでは、DataSet 内にキー列情報が存在している必要があります。

作成済みの SqlCeCommand に UpdateCommand が割り当てられた場合、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, "Barbara", "Decker"})
    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, "Barbara", "Decker" });
    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();
}

関連項目

参照

SqlCeDataAdapter クラス

System.Data.SqlServerCe 名前空間

DeleteCommand

InsertCommand

SelectCommand