Mira, en este caso, debes darte cuenta que hay varios tipos de archivos(extensiones). En el caso de las imágenes puedes usar este código:
Usando una PictureBox
Private sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ms As New MemoryStream(ExtraerImagen("Select foto from usuario"))
Me.PictureBox1.Image = Image.FromStream(ms)
Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Function ExtraerImagen(ByVal consulta As String) As Byte()
Dim SqlSelect As String = consulta
Dim Command As New SqlCommand(SqlSelect, con)
Dim MyPhoto() As Byte = CType(Command.ExecuteScalar(), Byte())
Return MyPhoto
End Function
Y con un archivo pues sólo lo tienes que extraer y luego leer, para extraer usa este código:
private sub BtnExtraer_Click(sender As System.Object, e As System.EventArgs) Handles BtnExtraer.Click
Try
con.Open()
Dim ex As String
cmd.CommandText = "SELECT extension FROM archivoss WHERE id = " & Val(Me.TextBox2.Text)
cmd.Connection = con
Dim dr1 As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dr1.Read()
ex = dr1.GetString(0)
dr1.Close()
con.Close()
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
con.Open()
Dim dire As String = Me.SaveFileDialog1.FileName & "." & ex
cmd.CommandText = "SELECT archivo FROM archivoss WHERE id = " & Val(Me.TextBox2.Text)
cmd.Connection = con
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dr.Read()
Dim bufferSize As Int32 = Convert.ToInt32(dr.GetBytes(0, 0, Nothing, 0, 0))
Dim aByte(bufferSize - 1) As Byte
dr.GetBytes(0, 0, aByte, 0, bufferSize)
dr.Close()
WriteBinaryFile(aByte, dire)
MsgBox("Exportado", MsgBoxStyle.Information, "Echo")
Me.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
con.Close()
End Try
con.Close()
End sub
Private Sub WriteBinaryFile(ByVal aByte() As Byte, ByVal fileName As String)
If (aByte Is Nothing) Then _
Throw New ArgumentNullException("aByte", "No se ha especificado ningún array de Bytes.")
If (String.IsNullOrEmpty(fileName)) Then _
Throw New ArgumentNullException("fileName", "No se ha especificado el archivo de destino.")
Try
If (File.Exists(fileName)) Then
If (MessageBox.Show("Ya existe un archivo con el mismo nombre. " & "¿Desea sobrescribirlo?", "Grabar archivo", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) = _
Windows.Forms.DialogResult.No) Then Return
File.Delete(fileName)
End If
Dim data As Long = aByte.Length
Dim tempFileName As String = Path.GetTempFileName()
Using fs As New FileStream(tempFileName, FileMode.OpenOrCreate)
Dim bw As New BinaryWriter(fs)
bw.Write(aByte, 0, Convert.ToInt32(data))
bw.Flush()
bw = Nothing
End Using
File.Move(tempFileName, fileName)
Catch ex As Exception
Throw
End Try
End Sub
Private Sub TextBox2_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
Dim Sep = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator
If Not (Char.IsNumber(e.KeyChar) Or e.KeyChar.Equals(Sep) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
|