OdbcDataAdapter.InsertCommand Propiedad
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene o establece una instrucción SQL o un procedimiento almacenado que se usa para insertar registros nuevos en el origen de datos.
public:
property System::Data::Odbc::OdbcCommand ^ InsertCommand { System::Data::Odbc::OdbcCommand ^ get(); void set(System::Data::Odbc::OdbcCommand ^ value); };
public System.Data.Odbc.OdbcCommand? InsertCommand { get; set; }
public System.Data.Odbc.OdbcCommand InsertCommand { get; set; }
member this.InsertCommand : System.Data.Odbc.OdbcCommand with get, set
Public Property InsertCommand As OdbcCommand
Valor de propiedad
Que OdbcCommand se usa durante una operación de actualización para insertar registros en el origen de datos que corresponden a nuevas filas de DataSet.
Ejemplos
En el ejemplo siguiente se crea y OdbcDataAdapter se establecen las SelectCommand propiedades y InsertCommand . Se supone que ya ha creado un OdbcConnection objeto .
public static OdbcDataAdapter CreateDataAdapter(
OdbcConnection connection)
{
string selectCommand =
"SELECT CustomerID, CompanyName FROM Customers";
OdbcDataAdapter adapter = new OdbcDataAdapter(
selectCommand, connection);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the Insert, Update and Delete commands.
adapter.InsertCommand = new OdbcCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (?, ?)");
adapter.UpdateCommand = new OdbcCommand(
"UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
"WHERE CustomerID = ?");
adapter.DeleteCommand = new OdbcCommand(
"DELETE FROM Customers WHERE CustomerID = ?");
// Create the parameters.
adapter.InsertCommand.Parameters.Add("@CustomerID",
OdbcType.Char, 5, "CustomerID");
adapter.InsertCommand.Parameters.Add("@CompanyName",
OdbcType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@CustomerID",
OdbcType.Char, 5, "CustomerID");
adapter.UpdateCommand.Parameters.Add("@CompanyName",
OdbcType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
OdbcType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add("@CustomerID",
OdbcType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
return adapter;
}
Public Function CreateDataAdapter( _
ByVal connection As OdbcConnection) As OdbcDataAdapter
Dim selectCommand As String = _
"SELECT CustomerID, CompanyName FROM Customers"
Dim adapter As OdbcDataAdapter = _
New OdbcDataAdapter(selectCommand, connection)
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
' Create the Insert, Update and Delete commands.
adapter.InsertCommand = New OdbcCommand( _
"INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (?, ?)")
adapter.UpdateCommand = New OdbcCommand( _
"UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _
"WHERE CustomerID = ?")
adapter.DeleteCommand = New OdbcCommand( _
"DELETE FROM Customers WHERE CustomerID = ?")
' Create the parameters.
adapter.InsertCommand.Parameters.Add( _
"@CustomerID", OdbcType.Char, 5, "CustomerID")
adapter.InsertCommand.Parameters.Add( _
"@CompanyName", OdbcType.VarChar, 40, "CompanyName")
adapter.UpdateCommand.Parameters.Add( _
"@CustomerID", OdbcType.Char, 5, "CustomerID")
adapter.UpdateCommand.Parameters.Add( _
"@CompanyName", OdbcType.VarChar, 40, "CompanyName")
adapter.UpdateCommand.Parameters.Add( _
"@oldCustomerID", OdbcType.Char, 5, "CustomerID").SourceVersion = _
DataRowVersion.Original
adapter.DeleteCommand.Parameters.Add( _
"@CustomerID", OdbcType.Char, 5, "CustomerID").SourceVersion = _
DataRowVersion.Original
Return adapter
End Function
Comentarios
Cuando la InsertCommand propiedad se asigna a un objeto creado OdbcCommand anteriormente, OdbcCommand no se clona. En su lugar, InsertCommand mantiene una referencia al objeto creado OdbcCommandanteriormente.
Durante una operación de actualización, si InsertCommand no se establece y la información de clave principal está presente en DataSet, puede usar la OdbcCommandBuilder clase para generar InsertCommandautomáticamente y comandos adicionales necesarios para conciliar con DataSet el origen de datos. Para ello, establezca la SelectCommand propiedad de .OdbcDataAdapter La lógica de generación también requiere que la información de columna clave esté presente en .DataSet Para obtener más información, vea Generar comandos con CommandBuilders.
Nota:
Si la ejecución de este comando devuelve filas, estas filas se pueden agregar a DataSet en función de cómo establezca la UpdatedRowSource propiedad del OdbcCommand objeto.