我正在遵循其他帖子中描述的方法来存储和检索 MSSQL 2008 数据库中的任何文件。一切正常,除了当我尝试将文件保存到磁盘上的任何位置时,我得到 UnauthorizedAccessException。以下是到目前为止所做的事情
- 硬编码“C:\Temp”和“D:\”的路径 - UnauthorizedAccessException
- 尝试以管理员身份运行 build .exe - 程序意外关闭
谁能指出我正确的方向来解决这个问题?
我在 Windows 8 上,这是我正在使用的代码;
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