Jump to content

Recommended Posts

Posted

Ola a todos!

Queria saber como fazer para armazenar um arquivo de videos no banco de dados access usando visual.net ?

Tenho um formulário de cadastro pronto no visual studio e todos os outros compros estão pronto só falta eu conseguir 

um método de armazena vídeos no banco de dados. Obrigado!

  • Vote 1
  • 3 months later...
Posted

Boa noite,

Quase qualquer tipo de ficheiro pode ser convertido em stream e ser guardado numa coluna tipo OLE Object. algo assim:

 Private Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Dim fdlgbox As New OpenFileDialog()
        fdlgbox.Filter = "All Files (.*)|*.*"
      
        fdlgbox.ShowDialog()

        If fdlgbox.FileName.Trim() <> "" Then
            Dim ofile() As Byte = Nothing
            ofile = GetStream(fdlgbox.FileName.Trim())
            SaveToDb(fdlgbox.SafeFileName.Trim(), ofile) 'Get Just FileName with Extention
        End If
    End Sub
    Private Function SaveToDb(strFileName As string ,fileStream As Byte()) As Boolean 
        'your saving process here (entity framework, ado.net, dapper etc)
    End Function
    Public Shared Function GetStream(ByVal filePath As String) As Byte()
        Dim buffer() As Byte
        Dim fileStream As New FileStream(filePath, FileMode.Open, FileAccess.Read)
        Try
            Dim length As Integer = CInt(Fix(fileStream.Length)) ' get file length
            buffer = New Byte(length - 1){} ' create buffer
            Dim count As Integer ' actual number of bytes read
            Dim sum As Integer = 0 ' total number of bytes read

            ' read until Read method returns 0 (end of the stream has been reached)
            count = fileStream.Read(buffer, sum, length - sum)
            Do While count > 0
                sum += count ' sum is a buffer offset for next reading
                count = fileStream.Read(buffer, sum, length - sum)
            Loop
        Finally
            fileStream.Close()
        End Try
        Return buffer
    End Function
  • Vote 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.