Boletín mugperu Digital - Julio 2009!
  Search 
Monday, May 21, 2012 ..:: Foros de Discusión ::.. Register  Login
Foros MUGPERU Minimize
Subject: Leer y Escribir Imagenes en un Campo de una tabla de SQL Server
Prev Next
You are not authorized to post a reply.

Author Messages
Lduenas
Posts:25

11/04/2005 11:53 AM  
Los campos que contienen imagenes se trabajan como un array de bytes (BLOB).
A continuacion un fragmento de código para leer y escribir imagenes en un campo de SQL:
 
Obtener valores BLOB a partir de una base de datos
=========================================
Dim pubsConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;")
Dim logoCMD As SqlCommand = New SqlCommand("SELECT pub_id, logo FROM pub_info", pubsConn)
Dim fs As FileStream                 ' Writes the BLOB to a file (*.bmp).
Dim bw As BinaryWriter               ' Streams the binary data to the FileStream object.
Dim bufferSize As Integer = 100      ' The size of the BLOB buffer.
Dim outbyte(bufferSize - 1) As Byte  ' The BLOB byte() buffer to be filled by GetBytes.
Dim retval As Long                   ' The bytes returned from GetBytes.
Dim startIndex As Long = 0           ' The starting position in the BLOB output.
Dim pub_id As String = ""            ' The publisher id to use in the file name.
' Open the connection and read data into the DataReader.
pubsConn.Open()
Dim myReader As SqlDataReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess)
Do While myReader.Read()
  ' Get the publisher id, which must occur before getting the logo.
  pub_id = myReader.GetString(0)
  ' Create a file to hold the output.
  fs = New FileStream("logo" & pub_id & ".bmp", FileMode.OpenOrCreate, FileAccess.Write)
  bw = New BinaryWriter(fs)
  ' Reset the starting byte for a new BLOB.
  startIndex = 0
  ' Read bytes into outbyte() and retain the number of bytes returned.
  retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
  ' Continue reading and writing while there are bytes beyond the size of the buffer.
  Do While retval = bufferSize
    bw.Write(outbyte)
    bw.Flush()
    ' Reposition the start index to the end of the last buffer and fill the buffer.
    startIndex += bufferSize
    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
  Loop
  ' Write the remaining buffer.
  bw.Write(outbyte, 0 , retval - 1)
  bw.Flush()
  ' Close the output file.
  bw.Close()
  fs.Close()
Loop
' Close the reader and the connection.
myReader.Close()
pubsConn.Close()
 
≈nbsp;
Escribir valores BLOB a partir de una base de datos
=========================================
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class EmployeeData
  Public Shared Sub Main()
    Dim hireDate As DateTime = DateTime.Parse("5/21/99")
    AddEmployee("Jones", "Mary", "Sales Representative", hireDate, 5, "jones.bmp")
  End Sub
  Public Shared Sub AddEmployee(lastName As String, firstName As String, title As String, hireDate As DateTime, _
                                reportsTo As Integer, photoFilePath As String)
    Dim photo() as Byte = GetPhoto(photoFilePath)
    Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;")
    Dim addEmp As SqlCommand = New SqlCommand("INSERT INTO Employees (LastName, FirstName, Title, HireDate, ReportsTo, Photo) " & _
                                              "Values(@LastName, @FirstName, @Title, @HireDate, @ReportsTo, @Photo)", nwindConn)
    addEmp.Parameters.Add("@LastName",  SqlDbType.NVarChar, 20).Value = lastName
    addEmp.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = firstName
    addEmp.Parameters.Add("@Title",     SqlDbType.NVarChar, 30).Value = title
    addEmp.Parameters.Add("@HireDate",  SqlDbType.DateTime).Value     = hireDate
    addEmp.Parameters.Add("@ReportsTo", SqlDbType.Int).Value          = reportsTo
    addEmp.Parameters.Add("@Photo",     SqlDbType.Image, photo.Length).Value = photo
    nwindConn.Open()
    addEmp.ExecuteNonQuery()
    nwindConn.Close()
  End Sub
  Public Shared Function GetPhoto(filePath As String) As Byte()
    Dim fs As FileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)
    Dim br As BinaryReader = new BinaryReader(fs)
    Dim photo() As Byte = br.ReadBytes(fs.Length)
    br.Close()
amp;nbsp;   fs.Close()
    Return photo
  End Function
End Class
Saludos:
Luis Dueñas
You are not authorized to post a reply.
Forums > Temas de Interés > Usando ADO.NET > Leer y Escribir Imagenes en un Campo de una tabla de SQL Server



ActiveForums 3.7
        
Copyright 2001-2012 MUGPERU   Terms Of Use  Privacy Statement