Export (0) Print
Expand All

OdbcDataAdapter.InsertCommand Property

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Gets or sets an SQL statement or stored procedure used to insert new records into the data source.

[Visual Basic]
Public Shadows Property InsertCommand As OdbcCommand
[C#]
public new OdbcCommand InsertCommand {get; set;}
[C++]
public: __property OdbcCommand* get_InsertCommand();
public: __property void set_InsertCommand(OdbcCommand*);
[JScript]
public hide function get InsertCommand() : OdbcCommand;
public function set InsertCommand(OdbcCommand);

Property Value

An OdbcCommand used during an update operation to insert records in the data source that correspond to new rows in the DataSet.

Remarks

When the InsertCommand property is assigned to a previously created OdbcCommand object, the OdbcCommand is not cloned. Instead, InsertCommand maintains a reference to the previously created OdbcCommand.

During an update operation, if InsertCommand is not set and primary key information is present in the DataSet, you can use the OdbcCommandBuilder class to automatically generate InsertCommand, and additional commands needed to reconcile the DataSet to the data source. To do this, set the SelectCommand property of the OdbcDataAdapter. The generation logic also requires key column information to be present in the DataSet. For more information see Automatically Generated Commands.

Note   If execution of this command returns rows, these rows may be added to the DataSet depending upon how you set the UpdatedRowSource property of the OdbcCommand object.

Example

[Visual Basic, C#, C++] The following example creates an OdbcDataAdapter and sets the SelectCommand and InsertCommand properties. It assumes you have already created an OdbcConnection object.

[Visual Basic] 
Public Shared Function CreateCustomerAdapter(conn As OdbcConnection) As OdbcDataAdapter 
  
  Dim da As OdbcDataAdapter = New OdbcDataAdapter()
  Dim cmd As OdbcCommand

  ' Create the SelectCommand.

  cmd = New OdbcCommand("SELECT * FROM Customers " & _
                       "WHERE Country = ? AND City = ?", conn)

  cmd.Parameters.Add("@Country", OdbcType.NVarChar, 15)
  cmd.Parameters.Add("@City", OdbcType.NVarChar, 15)

  da.SelectCommand = cmd

  ' Create the InsertCommand.

  cmd = New OdbcCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _
                       "VALUES (?, ?)", conn)

  cmd.Parameters.Add("@CustomerID", OdbcType.NChar, 5, "CustomerID")
  cmd.Parameters.Add("@CompanyName", OdbcType.NVarChar, 40, "CompanyName")

  da.InsertCommand = cmd

  Return da
End Function

[C#] 
public static OdbcDataAdapter CreateCustomerAdapter(OdbcConnection conn)
{
  OdbcDataAdapter da = new OdbcDataAdapter();
  OdbcCommand cmd;

  // Create the SelectCommand.

  cmd = new OdbcCommand("SELECT * FROM Customers " +
                       "WHERE Country = ? AND City = ?", conn);

  cmd.Parameters.Add("@Country", OdbcType.NVarChar, 15);
  cmd.Parameters.Add("@City", OdbcType.NVarChar, 15);

  da.SelectCommand = cmd;

  // Create the InsertCommand.

  cmd = new OdbcCommand("INSERT INTO Customers (CustomerID, CompanyName) " +
                       "VALUES (?, ?)", conn);

  cmd.Parameters.Add("@CustomerID", OdbcType.NChar, 5, "CustomerID");
  cmd.Parameters.Add("@CompanyName", OdbcType.NVarChar, 40, "CompanyName");

  da.InsertCommand = cmd;

  return da;
}

[C++] 
public:
    static OdbcDataAdapter* CreateCustomerAdapter(OdbcConnection* conn)
    {
        OdbcDataAdapter* da = new OdbcDataAdapter();
        OdbcCommand* cmd;

        // Create the SelectCommand.
        cmd = new OdbcCommand(S"SELECT * FROM Customers WHERE Country = ? AND City = ?", conn);

        cmd->Parameters->Add(S"@Country", OdbcType::NVarChar, 15);
        cmd->Parameters->Add(S"@City", OdbcType::NVarChar, 15);

        da->SelectCommand = cmd;

        // Create the InsertCommand.
        cmd = new OdbcCommand(S"INSERT INTO Customers (CustomerID, CompanyName) VALUES (?, ?)", conn);

        cmd->Parameters->Add(S"@CustomerID", OdbcType::NChar, 5, S"CustomerID");
        cmd->Parameters->Add(S"@CompanyName", OdbcType::NVarChar, 40, S"CompanyName");

        da->InsertCommand = cmd;

        return da;
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

OdbcDataAdapter Class | OdbcDataAdapter Members | System.Data.Odbc Namespace | DeleteCommand | SelectCommand | UpdateCommand

Show:
© 2015 Microsoft