Solved

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

Posted on 2013-06-17
2
390 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
Comment Utility
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
Comment Utility
>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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

12 Experts available now in Live!

Get 1:1 Help Now