1

I am following a method described in other post to store and retrieve any files from MSSQL 2008 database. Everything works well except when I try to save a file to any location on disk, I get the UnauthorizedAccessException. Here are the things that have done so far

  1. Hard coded the path to "C:\Temp" and "D:\" - UnauthorizedAccessException
  2. Tried running the build .exe as an administrator - program closed unexpectedly

Can anyone point me in the right direction to sort out this problem?

I am on Windows 8 and Here is the code I am using;

Public Sub downloadLearningObject(learningObjectID As Integer, folderPath As String) Implements ILearningObjectDAO.downloadLearningObject
    Dim connection As String = DatabaseConnection.ConnectionString
    Using con As New SqlConnection(connection)
        con.Open()
        Dim selectQuery As String = "SELECT File From LearningObject WHERE LearningObjectID=" & learningObjectID

        Dim cmd As New SqlCommand(selectQuery, con)
        Using sqlQueryResult = cmd.ExecuteReader()
            If sqlQueryResult IsNot Nothing Then
                sqlQueryResult.Read()
                Dim blob = New [Byte]((sqlQueryResult.GetBytes(0, 0, Nothing, 0, Integer.MaxValue)) - 1) {}
                sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length)

                'the following line is producing the exception
                Using fs = New FileStream(folderPath, FileMode.OpenOrCreate, FileAccess.ReadWrite) 
                    fs.Write(blob, 0, blob.Length)
                End Using
            End If
        End Using

    End Using
End Sub
Community
  • 1
  • 1
barack o mama
  • 129
  • 1
  • 2
  • 9

1 Answers1

0

I have found that the formatting of my "path" string is wrong. It had to be "C:\Temp\" instead of "C:\Temp". The missing backward slash at the end of the string triggered the exception.

barack o mama
  • 129
  • 1
  • 2
  • 9