none
Grabar imagen desde codigo vb.net en base de datos ORACLE y traer una imagen de Oracle a VB.NET RRS feed

  • Pregunta

  • Hola

    Estoy en un proyecto donde me piden grabar una imagen desde VB.NET a la base de datos en ORACLE y a la vez traer de Oracle la imagen a vb.net.

    Me pueden ayudar con eso, gracias


    guillermo leon rea

    miércoles, 22 de marzo de 2017 20:44

Respuestas

  • Hola:
    En un Form con 2 Button, copia y pega el siguiente código

    Imports System.IO
    Imports System.Data.SqlClient
    Public Class Form1
        Private Sub btnGrabar_Click(sender As Object, e As EventArgs) Handles btnGrabar.Click
            Dim ID As Integer = 1
            Dim PathImagen As String = "D:\TU_CARPETA\TU_FICHERO.JPG"
            Try
                InsertarFotoEnBDD(ID, PathImagen)
                MessageBox.Show("Se guardó la foto en la base de datos", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Private Sub InsertarFotoEnBDD(id As Integer, filefoto As String)
            Try
                Dim ms As MemoryStream = New MemoryStream()
                Dim fs As FileStream = New FileStream(filefoto, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                ms.SetLength(fs.Length)
                fs.Read(ms.GetBuffer(), 0, fs.Length)
                Dim arrImg() As Byte = ms.GetBuffer()
                ms.Flush()
                fs.Close()
                Using conn As New SqlConnection("TU_CADENA_DE_CONEXION")
                    Using cmd As SqlCommand = conn.CreateCommand()
                        conn.Open()
                        cmd.CommandText = "Insert Into IMAGENES ([ID], [NOMBRE], [IMAGEN]) Values ( @id, @nombre, @imagen)"
                        cmd.Parameters.Add("@id", SqlDbType.BigInt).Value = id
                        cmd.Parameters.Add("@nombre", SqlDbType.NVarChar, 64).Value = Path.GetFileName(filefoto)
                        cmd.Parameters.Add("@imagen", SqlDbType.VarBinary).Value = arrImg
                        cmd.ExecuteNonQuery()
                    End Using
                End Using
                ms.Close()
            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try
        End Sub
        Private Sub btnLeer_Click(sender As Object, e As EventArgs) Handles btnLeer.Click
            Dim ID As Integer = 1
            Try
                ObtenerFotoDeBDD(ID, "D:\temp\")
                MessageBox.Show("foto guardada con éxito")
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Private Sub ObtenerFotoDeBDD(id As Integer, savetofolder As String)
            Try
                Using conn As New SqlConnection("TU_CADENA_DE_CONEXION")
                    Using cmd As SqlCommand = conn.CreateCommand
                        conn.Open()
                        cmd.CommandText = "Select [nombre], [imagen] From IMAGENES Where [id]=@id"
                        cmd.Parameters.Add("@id", SqlDbType.BigInt).Value = id
                        Using reader As SqlDataReader = cmd.ExecuteReader
                            reader.Read()
                            Dim nombreFicheroBDD As String = reader.Item(0)
                            Dim nSave As String = savetofolder & nombreFicheroBDD
                            Dim arrImg() As Byte = reader.Item(1)
                            Dim ms As MemoryStream = New MemoryStream(arrImg)
                            Dim fs As FileStream = New FileStream(nSave, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
                            ms.WriteTo(fs)
                            fs.Flush()
                            fs.Close()
                            ms.Close()
                        End Using
                    End Using
                End Using
            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try
        End Sub

    End Class

    P.D.
    Tendras que cambiar los objetos SQL (SqlConnection, SqlCommand, etc) por los de Oracle (OracleConnection, OracleCommand, etc)

    Un saludo desde Bilbo
    Carlos

    jueves, 23 de marzo de 2017 12:05