Propriedade do RDL SqlCeDataAdapter.InsertCommand
Obtém ou define uma instrução SQL usada para inserir novos registros na fonte de dados.
Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (em System.Data.SqlServerCe.dll)
Sintaxe
'Declaração
Public Property InsertCommand As SqlCeCommand
Get
Set
'Uso
Dim instance As SqlCeDataAdapter
Dim value As SqlCeCommand
value = instance.InsertCommand
instance.InsertCommand = value
public SqlCeCommand InsertCommand { get; set; }
public:
property SqlCeCommand^ InsertCommand {
SqlCeCommand^ get ();
void set (SqlCeCommand^ value);
}
member InsertCommand : SqlCeCommand with get, set
function get InsertCommand () : SqlCeCommand
function set InsertCommand (value : SqlCeCommand)
Valor da propriedade
Tipo: System.Data.SqlServerCe.SqlCeCommand
Um SqlCeCommand usado durante a Update para inserir registros na fonte de dados que corresponde a novas linhas do DataSet.
Comentários
Durante uma chamada para Update, se esta propriedade não estiver definida e as informações da chave primária estiverem presentes no DataSet, InsertCommand poderá ser gerado automaticamente, se você definir a propriedade SelectCommand e usar o SqlCeCommandBuilder. Então, todos os comandos adicionais que não forem definidos serão gerados pelo SqlCeCommandBuilder. Essa lógica de geração exige que as informações da coluna de chave estejam presentes no DataSet.
Quando InsertCommand for atribuído a um SqlCeCommand criado anteriormente, o SqlCeCommand não será clonado. O InsertCommand mantém uma referência ao objeto SqlCeCommand criado anteriormente.
Dica
Se a execução deste comando retornar linhas, essas linhas podem ser adicionadas ao DataSet, dependendo de como é definida a propriedade UpdatedRowSource do objeto SqlCeCommand.
Exemplos
O exemplo a seguir cria um SqlCeDataAdapter e define algumas de suas propriedades.
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();
}