mb_force2 Ответов: 1

Может ли кто-нибудь помочь мне открыть файл?


Hello, I have a project, where I want to save pdf, doc, xls files in an ole object field in an accdb database. Until the part of saving and reading I could do, but then the part is missing to open, can someone help me?



Для сохранения файлов я написал этот код:

Private Builder As New OleDbConnectionStringBuilder With _
   { _
       .DataSource = IO.Path.Combine(Application.StartupPath, "SIGCB.accdb"), _
       .Provider = "Microsoft.ACE.OLEDB.12.0" _
   }

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
    Dim cmd As New OleDbCommand With {.Connection = cn}
    Dim dr As OleDbDataReader = Nothing
    Dim FileStream As System.IO.FileStream
    Dim Reader As System.IO.BinaryReader = Nothing
    Dim Data() As Byte = Nothing
    FileStream = New System.IO.FileStream(
        IO.Path.Combine(Application.StartupPath, Localização_do_Ficheiro_TB.Text),
        System.IO.FileMode.Open, System.IO.FileAccess.Read)
    Reader = New System.IO.BinaryReader(FileStream)
    Data = Reader.ReadBytes(CInt(FileStream.Length))
    cn.Open()
    cmd.CommandText = "INSERT INTO Data_Ficheiros (IDB_NINT, IDB_NMECAN, IDB_NOMEABV, ARQ_DATA, ARQ_DESC, ARQ_FILE) VALUES(@IDB_NINT,@IDB_NMECAN,@IDB_NOMEABV,@ARQ_DATA,@ARQ_DESC,@ARQ_FILE)"
    cmd.Connection = cn
    cmd.Parameters.Add("@IDB_NMECAN", System.Data.OleDb.OleDbType.WChar)
    cmd.Parameters(0).Value = Nº_Interno_TB.Text
    cmd.Parameters.Add("@IDB_NMECAN", System.Data.OleDb.OleDbType.WChar)
    cmd.Parameters(1).Value = Nº_Mecanográfico_TB.Text
    cmd.Parameters.Add("@IDB_NOMEABV", System.Data.OleDb.OleDbType.WChar)
    cmd.Parameters(2).Value = Nome_TB.Text
    cmd.Parameters.Add("@ARQ_DATA", System.Data.OleDb.OleDbType.WChar)
    cmd.Parameters(3).Value = Data_Ficheiro_TB.Text
    cmd.Parameters.Add("@ARQ_DESC", System.Data.OleDb.OleDbType.WChar)
    cmd.Parameters(4).Value = Descrição_Ficheiro_TB.Text
    cmd.Parameters.Add("@ARQ_FILE", System.Data.OleDb.OleDbType.LongVarBinary)
    cmd.Parameters(5).Value = Data
    cmd.ExecuteNonQuery()
    cn.Close()

End Sub


Что я уже пробовал:

А это для чтения файла

Dim cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
Dim cmd As New OleDbCommand With _
    { _
        .Connection = cn, _
        .CommandText = "SELECT ARQ_FILE FROM Data_Ficheiros Where ARQ_FILE= ARQ_FILE" _
    }
Dim dr As OleDbDataReader = Nothing
Dim FileStream As System.IO.FileStream
Dim Reader As OleDbDataReader
Dim Data() As Byte = Nothing
Dim Writer As System.IO.BinaryWriter = Nothing
Dim bufferSize As Integer = 1000
Dim buffer(bufferSize - 1) As Byte
Dim startIndex As Long = 0
Dim numberOfBytes As Long = 0
cn.Open()
Reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
Reader.Read()
FileStream = New System.IO.FileStream(
    IO.Path.Combine(Application.StartupPath, Localização_do_Ficheiro_TB.Text),
    System.IO.FileMode.Open, System.IO.FileAccess.Write)
Writer = New System.IO.BinaryWriter(FileStream)
Do
    numberOfBytes = Reader.GetBytes(0, startIndex, buffer, 0, bufferSize)
    If numberOfBytes = 0 Then
        Exit Do
    End If
    Writer.Write(buffer, 0, CInt(Fix(numberOfBytes)))
    startIndex += numberOfBytes
Loop While True
Writer.Flush()
If Writer IsNot Nothing Then
    Writer.Close()
End If
If FileStream IsNot Nothing Then
    FileStream.Close()
End If
If Reader IsNot Nothing Then
    Reader.Close()
End If
cn.Close()
MessageBox.Show("Done")


Теперь, как я его открою?

Richard MacCutchan

Пожалуйста, не повторяйте один и тот же вопрос. Мы уже ответили на этот вопрос в вашем оригинале.

Richard MacCutchan

И вы все еще не откорректированы инструкции Select.

1 Ответов

Рейтинг:
8

Dave Kreskowiak

Сохраните данные из базы данных в файл с соответствующим расширением и запустите его с помощью Класс Процесса (System.Диагностика)[^].