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 application 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 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 for the TdConnection.OpenFile event
    public void OnOpenFile(Object sender, TdSourceFileEventArgs eventArgs)
    {
        String FileName = eventArgs.FileName;
            
        // create a file stream
        _internalStream = new FileStream(FileName, FileMode.Open);
            
        // send the stream back to the Provider
        eventArgs.ExternalFileObject = _internalStream;
    }
             
    // Delegate for the TdConnection.CloseFile event
    public void OnCloseFile(Object sender, TdSourceFileEventArgs eventArgs)
    {
        _internalStream.Close();
    }
 }
            
            
public void CreateExternalProcedure(TdConnection cn)
{
    TdCommand cmd = cn.CreateCommand();
    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";
            
            
    // create delegates for the OpenFile and CloseFile events
    ExternalFileHandler fh = new ExternalFileHandler();
    TdOpenFileEventHandler extFile = new TdOpenFileEventHandler(fh.OnOpenFile);
    TdCloseFileEventHandler extReadCompleted = new TdCloseFileEventHandler(fh.OnCloseFile);
            
    // The OpenFile event will be raised when the Teradata Database requests the source code of
    // the stored procedure. Registration for this event is required if executing a DDL that 
    // requires data to be read from a file that resides on the client machine.
    cn.OpenFile += extFile;
            
    // The CloseFile event will be raised after the Teradata Database has received all the 
    // source code for the stored procedure. Registering for this event is optional.
    cn.CloseFile += extReadCompleted;
            
    try
    {
        // read the results
        using (TdDataReader dr = cmd.ExecuteReader())
        {
            String result;
            while (dr.Read() == true)
            {
                result = dr.GetString(0);
                Console.WriteLine(result);
            }
        }
    }
    finally
    {
        // unregister the event handlers
        cn.OpenFile -= extFile;
        cn.CloseFile -= extReadCompleted;
    }
}
Requirements
ProductVersionsPlatforms
.NET6, 7Windows, Linux, MacOS
.NET Framework4.6.2, 4.7, 4.7.1, 4.7.2, 4.8Windows
.NET Standard2.0Windows, Linux, MacOS
See Also