dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlDataAdapter Class / InsertCommand Property
Example

In This Topic
    InsertCommand Property (MySqlDataAdapter)
    In This Topic
    Gets or sets a MySQL statement to insert new records into the data source.
    Syntax
    'Declaration
     
    Public Shadows Property InsertCommand As MySqlCommand
    public new MySqlCommand InsertCommand {get; set;}

    Property Value

    A MySqlCommand used during an update operation to insert records in the data source that correspond to new rows in the System.Data.DataSet.
    Remarks
    During an update operation, if InsertCommand is not set and primary key information is present in the System.Data.DataSet, you can use the MySqlCommandBuilder class to automatically generate InsertCommand, and additional commands needed to reconcile the System.Data.DataSet to the data source. To do this, set the SelectCommand property of the MySqlDataAdapter. For more information see "Automatically Generated Commands" in the Microsoft .NET Framework SDK documentation.

    Note: If execution of this command returns rows, these rows may be added to the System.Data.DataSet depending upon how you set the Devart.Common.DbCommandBase.UpdatedRowSource property of the MySqlCommand object.

    Example
    The following example demonstrates how to retrieve and manipulate data using MySqlDataAdapter.
    public void UseDataAdapter(MySqlConnection myConnection)
    {
      MySqlDataAdapter myAdapter = new MySqlDataAdapter("SELECT DeptNo, DName FROM Test.Dept", myConnection);
      myAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
      DataSet myDataSet = new DataSet();
      myAdapter.Fill(myDataSet,"Departments");
      object[] rowVals = new object[2];
      rowVals[0] = 40;
      rowVals[1] = "Operations";
      myDataSet.Tables["Departments"].Rows.Add(rowVals);
      myAdapter.InsertCommand = new MySqlCommand("INSERT INTO Test.Dept (DeptNo, DName) " +
        "VALUES (:DeptNo, :DName)", myConnection);
            myAdapter.InsertCommand.Parameters.Add("DeptNo", MySqlType.Int, 0, "DeptNo");
      myAdapter.InsertCommand.Parameters.Add("DName", MySqlType.VarChar, 15, "DName");
      myAdapter.Update(myDataSet,"Departments"); 
      //Get all data from all tables within the dataset
      foreach(DataTable myTable in myDataSet.Tables)
      {
        foreach(DataRow myRow in myTable.Rows)
        {
          foreach (DataColumn myColumn in myTable.Columns)
          {
            Console.Write(myRow[myColumn]+"\t");
          }
          Console.WriteLine();
        }
        Console.WriteLine();
      }
    }
    Public Sub UseDataAdapter(ByVal myConnection As MySqlConnection)
      Dim myAdapter As MySqlDataAdapter = New MySqlDataAdapter("SELECT DeptNo, DName FROM Test.Dept", myConnection)
      myAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
      Dim myDataSet As DataSet = New DataSet
      myAdapter.Fill(myDataSet, "Departments")
      Dim rowVals(1) As Object
      rowVals(0) = "40"
      rowVals(1) = "Operations"
      myDataSet.Tables("Departments").Rows.Add(rowVals)
      myAdapter.InsertCommand = New MySqlCommand("INSERT INTO Test.Dept (DeptNo, DName) " & _
          "VALUES (:DeptNo, :DName)", myConnection)
      myAdapter.InsertCommand.Parameters.Add("DeptNo", MySqlType.Int, 0, "DeptNo")
      myAdapter.InsertCommand.Parameters.Add("DName", MySqlType.VarChar, 15, "DName")
      myAdapter.Update(myDataSet, "Departments")
      Dim myTable As DataTable
      Dim myRow As DataRow
      Dim myColumn As DataColumn
      ' Get all data from all tables within the dataset
      For Each myTable In myDataSet.Tables
        For Each myRow In myTable.Rows
          For Each myColumn In myTable.Columns
            Console.Write(myRow(myColumn) & Chr(9))
          Next myColumn
          Console.WriteLine()
        Next myRow
        Console.WriteLine()
      Next myTable
    End Sub
    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also