How do I insert an image from VB.net to Sql?

0

How do I insert an image from VB.net to Sql?

That's the way I'm using to save the other data

cmd2.CommandText = "insert into trabajadores (sueldo, acceso, login, password, 
            estado) values ('" + txtsueldo.Text + "','" + cmbacceso.Text + "','" + txtlogin.Text + "', encryptByPassPhrase('L$admin', 
            '" + txtpassword.Text + "'),'" + cmbestado.Text + "')"
    
asked by Anthony Pozo 19.09.2017 в 19:22
source

1 answer

1

Assuming that you are implementing a Windows Forms application, the following code that I separated in two steps could help you.

  • Select the image and call the function that performs the scuttle in BBDD:

    Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
        Dim openFileDialog As New OpenFileDialog()
        openFileDialog.CheckFileExists = True
        openFileDialog.AddExtension = True
        openFileDialog.Multiselect = True
        openFileDialog.Filter = "Image files (*.png)|*.jpg"
        Dim fichero As [Byte]() = Nothing
    
        If openFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Dim rutaArchivo As String = openFileDialog.FileNames(0)
            Dim fs = File.Open(rutaArchivo, FileMode.Open)
            fichero = New [Byte](fs.Length - 1) {}
            fs.Read(fichero, 0, CInt(fs.Length))
            fs.Close()
        End If
    
        If fichero IsNot Nothing Then
            Guardar(0.00, "test", "test", "test", "test", fichero)
        End If  
    End Sub 
    
  • Function where the guadado is performed in BBDD:

        Private connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Public Function Guardar(ByVal sueldo As Decimal, ByVal acceso As String, ByVal login As String, ByVal password As String, ByVal estado As String, ByVal fichero() As [Byte]) As Boolean
        Dim ok As Boolean = False
        Dim sql As String = "INSERT INTO Trabajadores (sueldo, acceso, login, password, estado, imagen)"
        sql += " VALUES (@sueldo, @acceso, @Fichero, @login, @password, , @estado,@imagen)"
    
        Try
            Using conexion As New SqlClient.SqlConnection(connection)
                Using comando As New SqlClient.SqlCommand(sql, conexion)
                    comando.Parameters.Add("@sueldo", SqlDbType.Decimal).Value = sueldo
                    comando.Parameters.Add("@acceso", SqlDbType.VarChar).Value = acceso
                    comando.Parameters.Add("@login", SqlDbType.VarChar).Value = login
                    comando.Parameters.Add("@password", SqlDbType.VarChar).Value = password
                    comando.Parameters.Add("@estado", SqlDbType.VarChar).Value = estado
                    comando.Parameters.Add("@imagen", SqlDbType.Binary).Value = fichero
                    conexion.Open()
                    comando.ExecuteNonQuery()
                    ok = True
                End Using
            End Using
        Catch ex As Exception
        End Try
    
        Return ok
    End Function    
    
  • answered by 21.09.2017 в 10:16