Esempio n. 1
0
        /// <summary>
        /// Connect to a stream of flat file data
        /// </summary>
        /// <param name="stream">The stream of data to connect to</param>
        /// <returns>If the data was valid and is a stream</returns>
        public override Boolean Connect(DataItemDefinition definition, DataConnection connection, Stream stream)
        {
            this.Connection = connection;      // Blank out the connection string as we are using a stream instead
            this.definition = definition;      // Assign the definition to use
            this.memoryData = new DataTable(); // Blank data by default

            // Do we have a stream and a definition
            if (stream != null && definition == null)
            {
                base.connected = true;
                return(true);
            }
            else if (stream != null && definition != null)
            {
                stream.Position = 0;   // Reset back to the start again in case someone else has read it
                base.MarkLastAction(); // Tell the provider base class that it did something

                // Read the data from the stream provided
                using (StreamReader textReader = new StreamReader(stream))
                {
                    this.memoryData = FixedWidthFileHelper.TextToDataTable(definition, connection, textReader.ReadToEnd());
                    base.connected  = true; // Mark the provider as connected
                    return(true);           // Connected without any errors
                }
            }

            return(false); // Failed if we get to here
        }
Esempio n. 2
0
        /// <summary>
        /// Commit the data back to the file
        /// </summary>
        /// <returns>If the write was successful</returns>
        public override Boolean Commit()
        {
            Boolean result = false; // Failed by default

            // Generate the flat file content based on the definition when connecting
            String flatFileContent = FixedWidthFileHelper.DataTableToString(this.definition, this.Connection, this.memoryData);

            // Try and write the file to disk
            try
            {
                // Get the processed connection string (with any injected items)
                String connectionString = this.Connection.ConnectionStringProcessed;

                // Write the file
                File.WriteAllText(connectionString, flatFileContent, definition.EncodingFormat);

                // Does the file now exist (and there were no errors writing)
                result = File.Exists(connectionString);
            }
            catch
            {
            }

            base.MarkLastAction(); // Tell the provider base class that it did something

            // Return the result
            return(result);
        }