• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

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

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
pamsauto
Asked:
pamsauto
1 Solution
 
pamsautoAuthor Commented:
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
 
CodeCruiserCommented:
>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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now