2012-01-26 78 views
0
Public Function storeFile(ByVal intSRID As Integer) As Byte() 
    Dim engine As New AccessEngine.DBEngine 
    Dim db As AccessEngine.Database = engine.OpenDatabase(DBFile) 
    Dim CommandText As String = "SELECT * From ServiceReport " & _ 
    "WHERE(((ServiceReport.[ServiceID]) = " & intSRID & "))" 

    Dim rs As AccessEngine.Recordset = db.OpenRecordset(CommandText, _ 
              AccessEngine.RecordsetTypeEnum.dbOpenDynaset, _ 
              Nothing, _ 
              AccessEngine.LockTypeEnum.dbOptimistic) 

    If Not rs.EOF Then 'Retrieve Attachments recordset 
     Dim rs2 As AccessEngine.Recordset2 = CType(rs.Fields("Attachment").Value, AccessEngine.Recordset2) 
     'A Recordset2 object contains all of the same properties and methods as the 
     'Recordset object. The Recordset2 object contains a new property, ParentRecordset, 
     'that support multi-valued field types. 

     If Not rs2.EOF Then 
      'You can attach a maximum of two gigabytes of data ' 
      '(the maximum size for an Access database). Individual 
      'files cannot exceed 256 megabytes in size. 

      Dim offsetbytes() As Byte 

      Dim row As DataRow = Nothing 
      Dim f2 As AccessEngine.Field2 
      Do While Not rs2.EOF 
       f2 = CType(rs2.Fields("FileData"), AccessEngine.Field2) 
       'get offset to data 
       offsetbytes = CType(f2.GetChunk(0, 4), Byte()) 
       offset = BitConverter.ToInt32(offsetbytes, 0) 

       Dim headerBytes() As Byte = CType(f2.GetChunk(0, offset), Byte()) 
       allbytes = CType(f2.Value, Byte()) 

       Dim databytes(0 To (allbytes.Length - offset) - 1) As Byte 
       Array.ConstrainedCopy(allbytes, offset, databytes, 0, allbytes.Length - offset) 

       strFilename = CType(rs2.Fields("FileName").Value, String) 

       bytesofdata = databytes 

       'If numbytes is greater than the number of bytes in the field, 
       'GetChunk returns the actual number of remaining bytes in the field. 
       'there appears to be a resource limit for GetChuck 
       'so use Int16.MaxValue for bytes to return 
       'ref: http://msdn.microsoft.com/en-us/library/ff194600.aspx 

       'I will use a memory stream for temporary storage 
       Dim ms As New IO.MemoryStream 
       Dim bw As New IO.BinaryWriter(ms) 
       Do 
        Dim returnBytes() As Byte = CType(f2.GetChunk(offset, Int16.MaxValue), Byte()) 
        If returnBytes IsNot Nothing Then 
         bw.Write(returnBytes) 
         offset = offset + returnBytes.Length 
        Else 
         Exit Do 
        End If 
       Loop 

       bw.Close() ' closes memory stream as well 
       rs2.MoveNext() 'Get next attachment 
      Loop 
     End If 'rs2.NoMatch 

    End If 'Not rs2.EOF 
    db.Close() 
    engine = Nothing 

    Return bytesofdata 

End Function 

這是我的代碼是在視覺工作室但工作時,我在IIS中運行它,我不斷收到此錯誤。 該錯誤總是出現在這行代碼:
Dim db As AccessEngine.Database = engine.OpenDatabase(DBFile)
錯誤:
Web服務工作,但不是在IIS 5.1

System.Runtime.InteropServices.COMException: The Microsoft Office Access database engine cannot open or write to the file 'E:\OSDB.accdb'. It is already opened exclusively by another user, or you need permission to view and write its data. 
     at Microsoft.Office.Interop.Access.Dao.DBEngineClass.OpenDatabase(String Name, Object Options, Object ReadOnly, Object Connect) 
     at CDBServiceR.storeFile(Int32 intSRID) in c:\inetpub\wwwroot\App_Code\CDBServiceR.vb:line 255 
     at RetrieveServiceRData.storeFile(Int32 intSRID) in c:\inetpub\wwwroot\App_Code\RetrieveServiceRData.vb:line 49 

我不知道如何擺脫這種error.Please幫助。

我的WebConfig:

<?xml version="1.0"?> 
<!-- 
For more information on how to configure your ASP.NET application, please visit 
http://go.microsoft.com/fwlink/?LinkId=169433 
--> 
<configuration> 
<system.web><compilation debug="true" strict="false" explicit="true"  targetFramework="4.0"> 
<assemblies> 
<add assembly="Microsoft.Office.Interop.Access.Dao, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/> 
<add assembly="ADODB, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/></assemblies></compilation> 
<webServices> 
<protocols> 
<add name="HttpGet"/> 
<add name="HttpPost"/> 
</protocols> 
</webServices> 
<!--<identity impersonate="true" userName="NONLOCKDOWN\IUSR_NONLOCKDOWN" password="password"/>--> 
</system.web> 
<appSettings> 
    <add key="DatabasePath" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\192.168.0.1\Share\tele.accdb;Persist Security Info=False;"/> 
</appSettings> 
</configuration>` 
+0

正如錯誤所述,iis進程很可能沒有訪問該位置的權限。如何解決這個問題取決於你的iis的版本和配置。如果你谷歌「授予iis權限」,你應該找到你需要的。 –

+0

我已授予完全控制ASPNET在共享和安全選項,但我仍然遇到此錯誤。 –

+0

我不認爲用戶是正確的IIS。它取決於你使用的版本,所以讓我們知道,或谷歌它。 –

回答

0

嘗試加入這一行的web.config

<webServices> 
     <protocols> 
      <add name="HttpGet"/> 
      <add name="HttpPost"/> 
     </protocols> 
    </webServices> 
</system.web> 

希望這有助於! :)

+0

我確實已經有了這些代碼行。 (: –

+0

我可以看看你的web.config一次,以便我可以幫你ü... – Madhu

+0

好吧,我已經添加了我的webconfig –

0

All Com+ components have "Enforce Security" checked, and we added a group called "users" and a user in that group called "NT AUTHORITY\Authenticated Users" that is used (This account is built in). This is the setup we have with IIS6 and Win2K3 Server currently.

+0

我使用IIS 5.1和WinXP所以我該如何去改變安全性? –

+0

請向web.config添加以下內容: ' ' – Mirodil

+0

該行代碼不起作用 –

相關問題