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 期間的 SqlCeCommand,可更新資料來源中與 DataSet 中的已修改資料列相對應的資料錄。
備註
在 Update 期間,如果尚未設定此屬性且 DataSet 中存在主索引鍵資訊,則可以自動產生 UpdateCommand。若要自動產生此屬性,請設定 SelectCommand 屬性並使用 SqlCeCommandBuilder。然後,任何未設定的其他命令會由 SqlCeCommandBuilder 產生。這個產生邏輯需要 DataSet 中存在索引鍵資料行資訊。
當 UpdateCommand 指派給先前建立的 SqlCeCommand 時,並不會複製 SqlCeCommand。UpdateCommand 會維護先前所建立之 SqlCeCommand 物件的參考。
注意
如果執行這個命令會傳回資料列,則這些資料列可以和 DataSet 合併,根據如何設定 SqlCeCommand 物件的 UpdatedRowSource 屬性而定。
範例
下列範例會建立 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();
}