Send feedback on this topic.
Teradata.Client.Provider
OpenFile Event
Example 



Teradata.Client.Provider Namespace > TdConnection Class : OpenFile Event
The .NET Data Provider for Teradata generates events when the Advanced SQL Engine requests the contents of a file.
Syntax
'Declaration
 
Public Event OpenFile As TdOpenFileEventHandler
'Usage
 
Dim instance As TdConnection
Dim handler As TdOpenFileEventHandler
 
AddHandler instance.OpenFile, handler
public event TdOpenFileEventHandler OpenFile
public:
event TdOpenFileEventHandler^ OpenFile
Event Data

The event handler receives an argument of type TdFileEventArgs containing data related to this event. The following TdFileEventArgs properties provide information specific to this event.

PropertyDescription
The name of the file that is requested by the Advanced SQL Engine. The name includes the path.  
The IO.Stream that was used to open the file.  
The type of file that was sent to Teradata (e.g. Source, Include, Object).  
The type of language that was used to write the function(e.g. C, CPP, Java).  
Remarks

The Advanced SQL Engine will request the contents of a file when a DDL statement is executed to create a function (e.g. stored procedure, UDF, UDT) and a file that resides on the client machine is specified. An example of a Teradata command that specifies an external file is as follows:

CREATE PROCEDURE ExternalProcedure(INOUT region VARCHAR(64)) LANGUAGE C NO SQL EXTERNAL NAME 'CS!ExternalProcedure!c:\xsp.c!F!ExternalProcedure' PARAMETER STYLE SQL;

An Application can only register with this event once. An InvalidOperationException will be thrown if an applcation attempts to register with this event more than once.

An application must register with this event if the Teradata command that is to be executed requires the application to provide the contents of a file. The data provider will require that the file be opened using a IO.Stream and sent back to the provider by setting the TdFileEventArgs.FileStream property.

Refer to the Advanced SQL Engine documentation, SQL Data Definition Language - Syntax and Examples, for more information on "creating external functions".

Example
// Contains the delegate used for the "read file" event that creates
// a stream to the external file and the delegate that registers 
// with the "read complete" event to close the stream.  
public class ExternalFileHandler
{
    // the stream that will be used to read the file that contains 
    // the stored procedure
    FileStream _internalStream;
            
    // Delegate that is invoked when the TdConnection.OpenFile 
    // event gets invoked by the provider
    public void OnFile(Object sender, TdSourceFileEventArgs eventArgs)
    {
        String FileName = eventArgs.FileName;
            
        // creating a stream to the file.
        _internalStream = new FileStream(FileName, FileMode.Open);
            
        // sending stream back to provider
        eventArgs.ExternalFileObject = _internalStream;
    }
             
    // Delegate that is invoked when the TdConnection.CloseFile
    // event gets invoked by the provider.
    public void OnCloseFile(Object sender, TdSourceFileEventArgs eventArgs)
    {
        _internalStream.Close();
    }
 }
            
            
public void CreateExternalProcedure(TdConnection cn)
{
    TdCommand cmd = cn.CreateCommand();
            
    // Creating the type that contains the callbacks that will be used by the 
    // ExternalFile and ExternalFileReadCompleted events
    ExternalFileHandler fh = new ExternalFileHandler();
            
   cmd.CommandText = "CREATE PROCEDURE provider_sp(INOUT region VARCHAR(64)) " +
                      "LANGUAGE C NO SQL EXTERNAL NAME 'CS!provider_sp!c:\xsp.c!F!provider_sp' " +
                      "PARAMETER STYLE SQL";
            
   // creating delegates for the OpenFile and CloseFile events
   TdOpenFileEventHandler extFile = new TdOpenFileEventHandler(fh.OnFile);
   TdCloseFileEventHandler extReadCompleted = 
            new TdCloseFileEventHandler(fh.OnCloseFile);
            
    // this event will get raised when Teradata requests the source code of the 
    // stored procedure.  Registration to this event is required if a DDL is 
    // executed that specifies data is to be read from a file that resides
    // on the client machine.
    cn.OpenFile += extFile;
            
    // this event will get raised after Teradata has received all the source code 
    // for the stored procedure.  Registering to this event is optional.
    cn.CloseFile += extReadCompleted;
            
    try
    {
        // going to read through the compiler messages sent from Teradata
        using (TdDataReader dr = cmd.ExecuteReader())
        {
            String result;
            
            while (dr.Read() == true)
            {
                result = dr.GetString(0);
            
                Console.WriteLine(result);
            }
        }
    }
    finally
    {
        // need to remove delegates from the events.
        cn.OpenFile -= extFile;
        cn.CloseFile -= extReadCompleted;
    }
}
Requirements

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

See Also