2014-02-07 19 views
1

我下面的其他post描述的存储和检索MSSQL 2008数据库的任何文件的方法时UnauthorizedAccessException。除了当我尝试将文件保存到磁盘上的任何位置时,一切运行良好,我得到UnauthorizedAccessException。以下是到目前为止已经完成VB.NET - 试图写一个文件来驱动

  1. 硬编码的路径事情“C:\ TEMP”和“d:\” -
  2. 尝试运行生成.exe作为管理员UnauthorizedAccessException - 程序意外关闭

任何人都可以点我在正确的方向来解决这个问题呢?

我在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 

回答

0

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

相关问题