Free Support Forum - aspose.com

Concatenate using SqlFileStream takes longer

I'm trying to merge mutiple PDF into single PDF. To do that i'm using PdfEditor.Concatenate() method. My files are stored in Sql database using FileStream. To read the file from the database i'm using SqlFileStream class that comes with .Net and its derived from System.IO.Stream class.
The concatenate method takes array of Stream objects, so i can pass array of the SqlFileStream instances.
It works fine but it REALLy take long time to concatenate. The two sample files i'm trying to merge are 216 pages and 6 pages each.

Any idea why. Below is my code?

public void FetchFiles_Using_SqlFileStream_And_Merge(Stream output)
        {
            SqlCommand command = null;
            Stream[] inputStreams = null;
            try
            {
                using (System.Data.SqlClient.SqlConnection connection =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["ArchiveCon"].ConnectionString))
                {
                    //The first task is to retrieve the file path
                    //of the SQL FILESTREAM BLOB that we want to
                    //access in the application.                                
                    command = GetCommand("Merge Pdf", connection, false);
                    List<string> files = new List<string>();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                            files.Add((string)reader["FilePath"]);
                    }
                <SPAN style="COLOR: green">//The next task is to obtain a transaction</SPAN>
                <SPAN style="COLOR: green">//context. All FILESTREAM BLOB operations</SPAN>
                <SPAN style="COLOR: green">//occur within a transaction context to</SPAN>
                <SPAN style="COLOR: green">//maintain data consistency.                    </SPAN>
                <SPAN style="COLOR: blue">byte</SPAN>[] transactionContext = GetTransactionContext(command);

                <SPAN style="COLOR: green">//The next step is to obtain SqlFileStream for each file</SPAN>
                inputStreams = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">Stream</SPAN>[files.Count];
                <SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> i = 0; i < files.Count; i++)
                {
                    <SPAN style="COLOR: #2b91af">SqlFileStream</SPAN> sqlFileStream = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">SqlFileStream</SPAN>(files[i], transactionContext, System.IO.<SPAN style="COLOR: #2b91af">FileAccess</SPAN>.Read);
                    sqlFileStream.Seek(0L, System.IO.<SPAN style="COLOR: #2b91af">SeekOrigin</SPAN>.Begin);
                    inputStreams[i] = sqlFileStream;
                }

                <SPAN style="COLOR: green">//Aspose Call                </SPAN>
                <SPAN style="COLOR: #2b91af">AsposeHelper</SPAN> aspose = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">AsposeHelper</SPAN>();
                aspose.Concatenate(inputStreams, output);

                <SPAN style="COLOR: green">//FILESTREAM must be closed. This closes the c# SqlFileStream.                 </SPAN>
                CloseStreams(inputStreams);

                command.Transaction.Commit();
            }
        }
        <SPAN style="COLOR: blue">catch</SPAN> (<SPAN style="COLOR: #2b91af">Exception</SPAN> ex)
        {
            <SPAN style="COLOR: green">//in case of failure before roolback we have to close streams.</SPAN>
            CloseStreams(inputStreams);
            <SPAN style="COLOR: green">//Rollback transation</SPAN>
            command.Transaction.Rollback();

            <SPAN style="COLOR: blue">throw</SPAN> ex;
        }
    }</PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"> </PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"><SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: #2b91af">SqlCommand</SPAN> GetCommand(<SPAN style="COLOR: blue">string</SPAN> operation, <SPAN style="COLOR: #2b91af">SqlConnection</SPAN> connection, <SPAN style="COLOR: blue">bool</SPAN> usingMemoryStream)
    {
        connection = <SPAN style="COLOR: blue">new</SPAN> System.Data.SqlClient.<SPAN style="COLOR: #2b91af">SqlConnection</SPAN>(<SPAN style="COLOR: #2b91af">ConfigurationManager</SPAN>.ConnectionStrings[<SPAN style="COLOR: #a31515">"ArchiveCon"</SPAN>].ConnectionString);
        <SPAN style="COLOR: #2b91af">SqlCommand</SPAN> command = connection.CreateCommand();
        command.CommandType = System.Data.<SPAN style="COLOR: #2b91af">CommandType</SPAN>.Text; 
        command.CommandText = <SPAN style="COLOR: #a31515">"SELECT [FileData].PathName() AS FilePath FROM Files WHERE FileFormat = '.pdf'"</SPAN>;
        connection.Open(); 
        <SPAN style="COLOR: blue">return</SPAN> command;
    }</PRE></PRE>