SqlCeDataAdapter.UpdateCommand 속성
데이터 원본에서 레코드를 업데이트하는 데 사용되는 SQL 문을 가져오거나 설정합니다.
네임스페이스: 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
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, "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();
}