Send feedback on this topic.
Teradata.Client.Provider
RecordsAffected Property
Example 



Teradata.Client.Provider Namespace > TdDataReader Class : RecordsAffected Property
Gets the number of rows changed, inserted, deleted or updated by execution of the SQL statement.
Syntax
'Declaration
 
Public Overrides NotOverridable ReadOnly Property RecordsAffected As Integer
'Usage
 
Dim instance As TdDataReader
Dim value As Integer
 
value = instance.RecordsAffected
public override int RecordsAffected {get;}
public:
property int RecordsAffected {
   int get() override;
}
Remarks

The records affected is cumulative. For example, if a request contains two insert statements the RecordsAffected will be 2.

A -1 will be returned for statements that are not DELETE, INSERT, or UPDATE.

If the number of rows affected from the execution of the SQL statements overflows Int32.MaxValue, Int32.MaxValue value will be returned. The provider will post an event to TdConnection.InfoMessage when an overflow condition occurs. The event will include a warning code and message. The warning message will have the actual number of records affected. The message code is 114004 and the message text is "An overflow occurred while calculating the number of records affected, actual value is [RecordsAffected]"

NOTE: In order for an application to be aware that an overflow occurred, it must register for the callback with TdConnection.InfoMessage event.

Example

The following example creates a TdConnection, sets up the TdConnection.InfoMessage event and executes a SQL statement.

When TdDataReader.RecordsAffected is used and if the number of records affected overflows Int32.MaxValue, Int32.MaxValue is returned and a warning message with the actual records affected is exposed through the TdConnection InfoMessage event.

When TdDataReader.RecordsAffected64 is used and if the number of records affected overflows Int64.MaxValue, Int64.MaxValue is returned and a warning message with the actual records affected is exposed through the TdConnection InfoMessage event.

// This method is invoked when the TdConnection.InfoMessage is fired.
public void OnInfoMessage(Object sender, TdInfoMessageEventArgs i)
{
    // The TdConnection.InfoMessage event gets fired when the records affected
    // overflows Int32.MaxValue for RecordsAffected and when the records affected
    // overflows Int64.MaxValue for RecordsAffected64. The actual number of
    // records affected can be extracted from the message as below. 
    if( i.Errors[0].Number == 114004 )
    {
        Decimal recordsAffected = 
                  Convert.ToDecimal(i.Message.Split('[')[3].Split(']')[0]);
    
        Console.WriteLine("Actual records affected is : ", recordsAffected);
    }
}
             
public void ExecuteDataReader(string connectionString, string commandText)
{
    // Open a session to Teradata
    TdConnection cn = new TdConnection(connectionString);
    
    // Setting up the event 
    cn.InfoMessage += new TdInfoMessageEventHandler(this.OnInfoMessage);
    
    cn.Open();
            
    // Execute the command
    TdCommand cmd = new TdCommand(commandText, cn);
    TdDataReader reader = cmd.ExecuteReader();
    Console.WriteLine("{0} records affected.", reader.RecordsAffected);
 
    // Close the command and session
    reader.close();
    cmd.Dispose();
    cn.Close();
            
}
Requirements

Target Platforms: Windows 8.1, Windows 10, Windows Server 2012 R2, Windows Server 2016, Windows Server 2019

See Also

Reference

TdDataReader Class
TdDataReader Members