GRABAR FOTO VB.NET Y SQL SERVER
Creamos la siguiente base de datos y tabla en el MS SQL Server 2012
Diseñamos el siguiente Formulario en una Aplicación WINDOWS de Visual Basic 2010
Consideramos el siguiente Código fuente:
------------------------------------------------------------------------------------------------------------
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim cn As New SqlConnection("Server=.; DataBase=DEMO; Integrated Security=true;")
Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click
Try
Dim cmd As New SqlCommand()
With cmd
.CommandText = "INSERT INTO EMPLEADOS(NOMBRE, APELLIDO, FECHANACIMIENTO, ESTADOCIVIL, FOTO)VALUES(@NOM,@APE,@FN,@EC,@FOTO)"
.Parameters.AddWithValue("@NOM", Me.txtnom.Text.Trim)
.Parameters.AddWithValue("@APE", Me.txtape.Text.Trim)
.Parameters.AddWithValue("@FN", Me.dtpfn.Value)
.Parameters.AddWithValue("@EC", Me.cboec.Text)
.Parameters.Add("@FOTO", System.Data.SqlDbType.Image)
Dim ms As New System.IO.MemoryStream()
picImagenEmpleado.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
.Parameters("@FOTO").Value = ms.GetBuffer()
.CommandType = CommandType.Text
.Connection = cn
cn.Open()
.ExecuteNonQuery()
MsgBox("Se registro satisfactoriamente", MsgBoxStyle.Information, "VB")
.Dispose()
cn.Close()
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Grabar:")
End Try
End Sub
Private Sub btnBuscarImagen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarImagen.Click
'llenar imagen a PictureBox
Using OpenFileDialog1 As New OpenFileDialog()
With OpenFileDialog1
.CheckFileExists = True
.ShowReadOnly = False
.Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
.FilterIndex = 2
If .ShowDialog = DialogResult.OK Then
picImagenEmpleado.ImageLocation = .FileName
End If
End With
End Using
End Sub
Diseñamos el siguiente Formulario en una Aplicación WINDOWS de Visual Basic 2010
Consideramos el siguiente Código fuente:
------------------------------------------------------------------------------------------------------------
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim cn As New SqlConnection("Server=.; DataBase=DEMO; Integrated Security=true;")
Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click
Try
Dim cmd As New SqlCommand()
With cmd
.CommandText = "INSERT INTO EMPLEADOS(NOMBRE, APELLIDO, FECHANACIMIENTO, ESTADOCIVIL, FOTO)VALUES(@NOM,@APE,@FN,@EC,@FOTO)"
.Parameters.AddWithValue("@NOM", Me.txtnom.Text.Trim)
.Parameters.AddWithValue("@APE", Me.txtape.Text.Trim)
.Parameters.AddWithValue("@FN", Me.dtpfn.Value)
.Parameters.AddWithValue("@EC", Me.cboec.Text)
.Parameters.Add("@FOTO", System.Data.SqlDbType.Image)
Dim ms As New System.IO.MemoryStream()
picImagenEmpleado.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
.Parameters("@FOTO").Value = ms.GetBuffer()
.CommandType = CommandType.Text
.Connection = cn
cn.Open()
.ExecuteNonQuery()
MsgBox("Se registro satisfactoriamente", MsgBoxStyle.Information, "VB")
.Dispose()
cn.Close()
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Grabar:")
End Try
End Sub
Private Sub btnBuscarImagen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarImagen.Click
'llenar imagen a PictureBox
Using OpenFileDialog1 As New OpenFileDialog()
With OpenFileDialog1
.CheckFileExists = True
.ShowReadOnly = False
.Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
.FilterIndex = 2
If .ShowDialog = DialogResult.OK Then
picImagenEmpleado.ImageLocation = .FileName
End If
End With
End Using
End Sub
------------------------------------------------------------------------------------------------------------
Ejecute la Aplicación y el Realice lo siguiente:
Por. Ing. Jhonatan Abal Mejia
Comentarios