Solved

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

Posted on 2013-06-17
2
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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