Send feedback on this topic.
Teradata.Client.Provider
TdDataAdapter Support Example
.NET Data Provider for Teradata > Developer's Guide > Working with the Data Provider Features > Generated Data Retrieval (Identity Column Support) > TdDataAdapter Support Example

The code below illustrates an application utilizing the TdDataAdapter and setting the commands and parameters manually without a command builder. First a table with identity columns is created and populated with 2 rows - Customer1 and Customer2.  Teradata will supply the identity column values for the IdCol column. A TdDataAdapter is created with a select statement, which is used to fill the DataSet. 

The InsertCommand is set manually by creating he command text and setting the parameter list for the InsertCommand. The InsertCommand GeneratedDataBehavior must be set to either IdentityColumn or AllColumns to retrieve the updated identity column values from Teradata.  The InsertCommand UpdatedRowSource property is set to FirstReturnedRow, which enables the DataTable to be updated from the returned result set from Teradata. The default value created by TdCommand for the UpdatedRowSource property is Both, which indicates both the output parameters and the first row of a returned result set may be mapped to the changed row in the DataSet.

To validate the identity column values are populated correctly, the DataTable column values are printed.  The results appear after the reproduced code.

C#
Copy Code
// sql create identity column table statement with a primary key
String sqlCreateTable = @"create set table idColTbl " +
    @"(Custid integer, IdCol integer generated always as identity, CustName varchar(30), " +
    @"State char(2)) unique primary index (Custid)";

// sql drop table statement
String sqlDropTable = "Drop table idColTbl";

// sql insert statement
String sqlInsert =
    @"Insert into idColTbl values (10,,'Customer1','OH');" +
    @"Insert into idColTbl values (20,,'Customer2','KY');";

// sql select statement
String sqlSelect = @"Select * from idColTbl";

// Teradata connection
TdConnection myConnection = new TdConnection ("Data Source= X; userid= Y; password= Z;");

try
{
   myConnection.Open();

   // create the table
   TdCommand myCommand = new TdCommand (sqlCreateTable, myConnection);
   myCommand.ExecuteNonQuery();

   // insert 2 rows
   TdCommand myInsertCmd = new TdCommand (sqlInsert, myConnection);
   myInsertCmd.ExecuteNonQuery();

   // create a TdDataAdapter
   // and set the commands manually
   TdDataAdapter custDA = new TdDataAdapter (sqlSelect, myConnection);
   custDA.InsertCommand = new TdCommand ("insert into idColTbl values (?,?,?,?)", myConnection);

   // create the parameters
   TdParameter parameter = new TdParameter();
   parameter.SourceColumn = "Custid";
   parameter.SourceVersion = DataRowVersion.Current;
   parameter.TdType = TdType.Integer;
   custDA.InsertCommand.Parameters.Add (parameter);

   parameter = new TdParameter();
   parameter.SourceColumn = "IdCol";
   parameter.SourceVersion = DataRowVersion.Current;
   parameter.TdType = TdType.Integer;
   custDA.InsertCommand.Parameters.Add (parameter);

   parameter = new TdParameter();
   parameter.SourceColumn = "CustName";
   parameter.SourceVersion = DataRowVersion.Current;
   parameter.TdType = TdType.VarChar;
   parameter.Size = 30;
   custDA.InsertCommand.Parameters.Add (parameter);

   parameter = new TdParameter();
   parameter.SourceColumn = "State";
   parameter.SourceVersion = DataRowVersion.Current;
   parameter.TdType = TdType.Char;
   parameter.Size = 2;
   custDA.InsertCommand.Parameters.Add (parameter);

   custDA.InsertCommand.GeneratedDataBehavior = GeneratedDataBehavior.IdentityColumn;
   // Set the UpdatedRowSource property since the default sets this to None
   custDA.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;

   // create a new dataset
   DataSet custDS = new DataSet();

   // populate the DataSet from the select command
   custDA.Fill (custDS, "idColTbl");

   // Code to modify data in the DataSet here.
   DataRowCollection rows = custDS.Tables ["idColTbl"].Rows;
   DataColumnCollection columns = custDS.Tables ["idColTbl"].Columns;

   // we don't set the identity column here as it will be populated by
   // the generated data retrieval feature
   DataRow newRow = custDS.Tables ["idColTbl"].NewRow();
   newRow ["Custid"] = 30;
   newRow ["Custname"] = "NewCust1";
   newRow ["State"] = "CA";

   rows.Add newRow);
   newRow = custDS.Tables ["idColTbl"].NewRow();
   newRow ["Custid"] = 40;
   newRow ["Custname"] = "NewCust2";
   newRow ["State"] = "CA";

   // add the rows to the DataTable
   rows.Add (newRow);

   // the new rows will be added to the Advanced SQL Engine and the identity columns
   // will be returned by the SQL Engine
   int rowsPacted = custDA.Update (custDS, "idColTbl");

   int i = 0;

   // print out the values to confirm the updated identity columns
   foreach (DataRow selRow in rows)
   {
      Console.Write ("Row {0}", i++);
      foreach (DataColumn selCols in columns)
      {
         Console.Write(" {0} = {1}", selCols.ColumnName, selRow[selCols]);
      }
      Console.WriteLine();
   }

   // drop the table
   TdCommand cmdDropTable = new TdCommand (sqlDropTable, myConnection);
   cmdDropTable.ExecuteNonQuery();
}
catch (TdException e)
{
   Console.WriteLine ("Error {0} - {1}", e.ErrorCode, e.Message);

   TdCommand cmdDropTable = new TdCommand (sqlDropTable, myConnection);
   cmdDropTable.ExecuteNonQuery();

   throw;
}
finally
{
   // close the connection
   myConnection.Close();
}

The results of the above application are below.

     Row 0 -> Custid = 20 IdCol = 2 CustName = Customer2 State = KY
     Row 1 -> Custid = 10 IdCol = 1 CustName = Customer1 State = OH
     Row 2 -> Custid = 30 IdCol = 3 CustName = NewCust1 State = CA
     Row 3 -> Custid = 40 IdCol = 4 CustName = NewCust2 State = CA