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
Solved

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

Posted on 2013-06-17
2
408 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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