使用VB.Net删除附件字段

时间:2015-11-11 12:13:57

标签: vb.net ms-access

我希望能够通过VB.Net删除存储为Access数据库中附件的单个文件

我设法存储,打开和下载"从Access数据库中有效地工作,但最后一个难题是允许我的最终用户删除附件字段中的单个文件。

这似乎比其他选项困难得多,因为我似乎只能找到有关如何删除整个数据库条目的信息,而不仅仅是单个附件。如果对条目存储了多个附件,则会变得更加复杂。

我使用以下代码检查点击datagrid时会发生什么:

 If e.ColumnIndex = ViewFileColum.Index Then
        'Get the file name and contents for the selected attachment.
        Dim gridRow = childgrid.Rows(e.RowIndex)
        Dim tableRow = DirectCast(gridRow.DataBoundItem, DataRowView)
        Dim fileName = CStr(tableRow(FILE_NAME_COLUMN_NAME))
        Dim fileContents = GetFileContents(DirectCast(tableRow(FILE_DATA_COLUMN_NAME), Byte()))

        DisplayTempFile(fileName, fileContents)
    End If

    If e.ColumnIndex = DownloadFileColumn.Index Then
        'Get the file name and contents for the selected attachment.
        MoveFile = True
        Dim gridRow = childgrid.Rows(e.RowIndex)
        Dim tableRow = DirectCast(gridRow.DataBoundItem, DataRowView)
        Dim fileName = CStr(tableRow(FILE_NAME_COLUMN_NAME))
        Dim fileContents = GetFileContents(DirectCast(tableRow(FILE_DATA_COLUMN_NAME), Byte()))

        DisplayTempFile(fileName, fileContents)
    End If

我想添加第三部分,说明是否点击DeleteFileColumn按钮然后从数据库中删除该特定附件,但这似乎不可能。

在检索上述两个选项的信息时,我使用以下代码:

Dim tempFolderPath = Path.GetTempPath()
Dim tempFilePath = Path.Combine(tempFolderPath, fileName)

'If the specified file exists, add a number to the name to differentiate them.
If File.Exists(tempFilePath) Then
    Dim fileNumber = 1

    Do
        tempFilePath = Path.Combine(tempFolderPath,
                                    String.Format("{0} ({1}){2}",
                                                  Path.GetFileNameWithoutExtension(fileName),
                                                  fileNumber,
                                                  Path.GetExtension(fileName)))
        fileNumber += 1
    Loop Until Not File.Exists(tempFilePath)
End If

'Save the file and open it.

'If "DOWNLOAD" button is clicked
If MoveFile = True Then
    File.WriteAllBytes(SaveLocation & "\" & Path.GetFileNameWithoutExtension(fileName) & Path.GetExtension(fileName), fileContents)
    MoveFile = False


    'If "OPEN" button is clicked
Else
    File.WriteAllBytes(tempFilePath, fileContents)
    Dim attachmentProcess = Process.Start(tempFilePath)

    If attachmentProcess Is Nothing Then
        'Remember the file and try to delete it when this app closes.
        tempFilePaths.Add(tempFilePath)
    Else
        'Remember the file and try to delete it when the associated process exits.
        attachmentProcess.EnableRaisingEvents = True
        AddHandler attachmentProcess.Exited, AddressOf attachmentProcess_Exited
        tempFilePathsByProcess.Add(attachmentProcess, tempFilePath)
    End If
End If

此代码在打开信息之前复制信息,因此我实际上并没有直接处理数据库中的文件。我在网上找到的另一个例子中使用了这个代码,但我很难弄清楚如何在数据库上处理文件,或者它是否可能?

由于

1 个答案:

答案 0 :(得分:1)

您需要为附件字段创建Access DAO Recordset2对象,找到与要删除的特定附件相对应的记录,然后Delete()记录该记录。

以下示例将从ID=1的记录的附件字段中删除名为“testDocument.pdf”的文档:

' required COM reference:
'     Microsoft Office 14.0 Access Database Engine Object Library
' 
' Imports Microsoft.Office.Interop.Access.Dao
'
Dim dbe As New DBEngine
Dim db As Database = dbe.OpenDatabase("C:\Users\Public\Database1.accdb")
Dim rstMain As Recordset = db.OpenRecordset(
        "SELECT Attachments FROM AttachTest WHERE ID=1",
        RecordsetTypeEnum.dbOpenDynaset)
rstMain.Edit()
Dim rstAttach As Recordset2 = CType(rstMain.Fields("Attachments").Value, Recordset2)
Do Until rstAttach.EOF
    If rstAttach.Fields("FileName").Value.Equals("testDocument.pdf") Then
        rstAttach.Delete()
        Exit Do
    End If
    rstAttach.MoveNext()
Loop
rstAttach.Close()
rstMain.Update()
rstMain.Close()
db.Close()