Solved

SqlFileStream - Not streaming to correct file - Creating two files for each row inserted

Posted on 2013-06-17
2
396 Views
Last Modified: 2013-07-08
When I attempt to save / insert file data using SQLFileStream, it doesn't work as expected.  I watch the file system when doing my insert.  I can see an empty file get created when the database row is inserted. My understanding is that when I write to the file after obtaining a handle to it, that this empty file will get updated.  Instead, when New SqlFileStream() is called, a NEW file is created instead. Everytime.  This is of course causing problems when I try to access these files later on.  My code is below.  

                        PAMSEMAIL.Open()
                        Dim objSqlTran As SqlTransaction = PAMSEMAIL.BeginTransaction()

                        Dim objSqlCmd As New SqlCommand("AddFile_W32Stream", PAMSEMAIL, objSqlTran)
                        objSqlCmd.CommandType = CommandType.StoredProcedure

                        Dim objSqlParam1 As New SqlParameter("@MessagesId", SqlDbType.UniqueIdentifier)
                        objSqlParam1.Value = MessageId

                        Dim objSqlParam2 As New SqlParameter("@FileType", SqlDbType.VarChar, 50)
                        objSqlParam2.Value = ContentType

                        Dim objSqlParam3 As New SqlParameter("@FileName", SqlDbType.VarChar, 1000)
                        objSqlParam3.Value = FileName

                        Dim objSqlParam4 As New SqlParameter("@FileSize", SqlDbType.VarChar)
                        objSqlParam4.Value = FileSize

                        Dim objSqlParam5 As New SqlParameter("@VisibleAsAttachment", SqlDbType.Bit)
                        objSqlParam5.Value = VisibleAsAttachment

                        Dim objSqlParamOutput As New SqlParameter("@AttachmentId", SqlDbType.UniqueIdentifier, -1)
                        objSqlParamOutput.Direction = ParameterDirection.Output

                        objSqlCmd.Parameters.Add(objSqlParam1)
                        objSqlCmd.Parameters.Add(objSqlParam2)
                        objSqlCmd.Parameters.Add(objSqlParam3)
                        objSqlCmd.Parameters.Add(objSqlParam4)
                        objSqlCmd.Parameters.Add(objSqlParam5)
                        objSqlCmd.Parameters.Add(objSqlParamOutput)

                        objSqlCmd.ExecuteNonQuery()

                        ' At this point we should have a record with blank file data inserted to the database. We should also have an empty file in windows.
                        ' Next we will get the information about the inserted file to use to stream data into the file (placeholder)

                        Dim objInsertedSqlCmd = New SqlCommand("SELECT FileData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() from attachments where id = @AttachmentId", PAMSEMAIL, objSqlTran)
                        objInsertedSqlCmd.CommandType = CommandType.Text

                        objInsertedSqlCmd.Parameters.Add("@AttachmentId", SqlDbType.UniqueIdentifier).Value = objSqlCmd.Parameters("@AttachmentId").Value


                        Dim ContextReader As SqlDataReader = objInsertedSqlCmd.ExecuteReader(CommandBehavior.SingleRow)
                        ContextReader.Read()
                        Dim filePathName As SqlString = ContextReader.GetSqlString(0)
                        Dim fileToken As SqlBinary = ContextReader.GetSqlBinary(1)
                        ContextReader.Close()

                        ' When we execute this statement, a second file is generated. 

                        Dim objSqlFileStream As New SqlFileStream(filePathName.Value, fileToken.Value, FileAccess.Write)

                        objSqlFileStream.Write(buffer, 0, buffer.Length)
                        objSqlFileStream.Close()
                        objSqlTran.Commit()
                        PAMSEMAIL.Close()

Open in new window

File System image showing the file that we streamed data into and the original empty file that I expected the data to be streamed to.When I attempt to save / insert file data using SQLFileStream, it doesn't work as expected.  I watch the file system when doing my insert.  I can see an empty file get created when the database row is inserted. My understanding is that when I write to the file after obtaining a handle to it, that this empty file will get updated.  Instead, when New SqlFileStream() is called, a NEW file is created instead. Everytime.  This is of course causing problems when I try to access these files later on.  My code is below.  

                        PAMSEMAIL.Open()
                        Dim objSqlTran As SqlTransaction = PAMSEMAIL.BeginTransaction()

                        Dim objSqlCmd As New SqlCommand("AddFile_W32Stream", PAMSEMAIL, objSqlTran)
                        objSqlCmd.CommandType = CommandType.StoredProcedure

                        Dim objSqlParam1 As New SqlParameter("@MessagesId", SqlDbType.UniqueIdentifier)
                        objSqlParam1.Value = MessageId

                        Dim objSqlParam2 As New SqlParameter("@FileType", SqlDbType.VarChar, 50)
                        objSqlParam2.Value = ContentType

                        Dim objSqlParam3 As New SqlParameter("@FileName", SqlDbType.VarChar, 1000)
                        objSqlParam3.Value = FileName

                        Dim objSqlParam4 As New SqlParameter("@FileSize", SqlDbType.VarChar)
                        objSqlParam4.Value = FileSize

                        Dim objSqlParam5 As New SqlParameter("@VisibleAsAttachment", SqlDbType.Bit)
                        objSqlParam5.Value = VisibleAsAttachment

                        Dim objSqlParamOutput As New SqlParameter("@AttachmentId", SqlDbType.UniqueIdentifier, -1)
                        objSqlParamOutput.Direction = ParameterDirection.Output

                        objSqlCmd.Parameters.Add(objSqlParam1)
                        objSqlCmd.Parameters.Add(objSqlParam2)
                        objSqlCmd.Parameters.Add(objSqlParam3)
                        objSqlCmd.Parameters.Add(objSqlParam4)
                        objSqlCmd.Parameters.Add(objSqlParam5)
                        objSqlCmd.Parameters.Add(objSqlParamOutput)

                        objSqlCmd.ExecuteNonQuery()

                        ' At this point we should have a record with blank file data inserted to the database. We should also have an empty file in windows.
                        ' Next we will get the information about the inserted file to use to stream data into the file (placeholder)

                        Dim objInsertedSqlCmd = New SqlCommand("SELECT FileData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() from attachments where id = @AttachmentId", PAMSEMAIL, objSqlTran)
                        objInsertedSqlCmd.CommandType = CommandType.Text

                        objInsertedSqlCmd.Parameters.Add("@AttachmentId", SqlDbType.UniqueIdentifier).Value = objSqlCmd.Parameters("@AttachmentId").Value


                        Dim ContextReader As SqlDataReader = objInsertedSqlCmd.ExecuteReader(CommandBehavior.SingleRow)
                        ContextReader.Read()
                        Dim filePathName As SqlString = ContextReader.GetSqlString(0)
                        Dim fileToken As SqlBinary = ContextReader.GetSqlBinary(1)
                        ContextReader.Close()

                        ' When we execute this statement, a second file is generated. 

                        Dim objSqlFileStream As New SqlFileStream(filePathName.Value, fileToken.Value, FileAccess.Write)

                        objSqlFileStream.Write(buffer, 0, buffer.Length)
                        objSqlFileStream.Close()
                        objSqlTran.Commit()
                        PAMSEMAIL.Close()

Open in new window

0
Comment
Question by:pamsauto
2 Comments
 

Author Comment

by:pamsauto
ID: 39254687
I was thinking that perhaps there are two files being created due to a transaction problem.  So, to be certain it was not transaction related, I modified my stored procedure to just return the path and context as output parameters after the SP does the insert.   It still creates two files.  Here is that code...

                        PAMSEMAIL.Open()
                        Dim objSqlTran As SqlTransaction = PAMSEMAIL.BeginTransaction()


                        Dim objSqlCmd As New SqlCommand("AddFile_W32Stream", PAMSEMAIL, objSqlTran)
                        objSqlCmd.CommandType = CommandType.StoredProcedure

                        Dim objSqlParam1 As New SqlParameter("@MessagesId", SqlDbType.UniqueIdentifier)
                        objSqlParam1.Value = MessageId

                        Dim objSqlParam2 As New SqlParameter("@FileType", SqlDbType.VarChar, 50)
                        objSqlParam2.Value = ContentType

                        Dim objSqlParam3 As New SqlParameter("@FileName", SqlDbType.VarChar, 1000)
                        objSqlParam3.Value = FileName

                        Dim objSqlParam4 As New SqlParameter("@FileSize", SqlDbType.VarChar)
                        objSqlParam4.Value = FileSize

                        Dim objSqlParam5 As New SqlParameter("@VisibleAsAttachment", SqlDbType.Bit)
                        objSqlParam5.Value = VisibleAsAttachment

                        Dim objSqlParamOutput As New SqlParameter("@AttachmentId", SqlDbType.UniqueIdentifier, -1)
                        objSqlParamOutput.Direction = ParameterDirection.Output

                        Dim objSqlParamOutput2 As New SqlParameter("@FilePath", SqlDbType.VarChar, -1)
                        objSqlParamOutput2.Direction = ParameterDirection.Output

                        Dim objSqlParamOutput3 As New SqlParameter("@Context", SqlDbType.VarBinary, -1)
                        objSqlParamOutput3.Direction = ParameterDirection.Output

                        objSqlCmd.Parameters.Add(objSqlParam1)
                        objSqlCmd.Parameters.Add(objSqlParam2)
                        objSqlCmd.Parameters.Add(objSqlParam3)
                        objSqlCmd.Parameters.Add(objSqlParam4)
                        objSqlCmd.Parameters.Add(objSqlParam5)
                        objSqlCmd.Parameters.Add(objSqlParamOutput)
                        objSqlCmd.Parameters.Add(objSqlParamOutput2)
                        objSqlCmd.Parameters.Add(objSqlParamOutput3)

                        objSqlCmd.ExecuteNonQuery()


                        ' At this point we should have a record with blank file data inserted to the database. We should also have an empty file in windows.

                        Dim objSqlFileStream As New SqlFileStream(objSqlParamOutput2.Value, objSqlParamOutput3.Value, FileAccess.Write)

                        objSqlFileStream.Write(buffer, 0, buffer.Length)
                        objSqlFileStream.Close()
                        objSqlTran.Commit()
                        PAMSEMAIL.Close()

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39271648
>Instead, when New SqlFileStream() is called, a NEW file is created instead.

The FilePath supplied to SqlFileStream is logical path not physical path so when you create a new instance, it creates a new file.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now