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
- Hard coded the path to "C:\Temp" and "D:\" - UnauthorizedAccessException
- 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