1

我正在遵循其他帖子中描述的方法来存储和检索 MSSQL 2008 数据库中的任何文件。一切正常,除了当我尝试将文件保存到磁盘上的任何位置时,我得到 UnauthorizedAccessException。以下是到目前为止所做的事情

  1. 硬编码“C:\Temp”和“D:\”的路径 - UnauthorizedAccessException
  2. 尝试以管理员身份运行 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
4

1 回答 1

0

我发现我的“路径”字符串的格式是错误的。它必须是“C:\Temp\”而不是“C:\Temp”。字符串末尾缺少的反斜杠触发了异常。

于 2014-04-14T18:21:01.823 回答