2016-03-25 13 views
0

SqlDataReader kullanarak bir veritabanından kayıtları çeken bir .net uygulamasını değiştiriyorum. Ben SQL Server BCP queryout bir düz dosya ve 0E31 uygulama için BCP queryout veri varbinary(max) sütundaki her satırdan .jpg görüntüleri oluşturmak için düz dosyaları tüketir iki parçalı bir işlemle değiştiriyorum . Benim durumumdaBCP sorgulama görüntüleri varbinary (max) içinde saklanan

(byte[])reader[field]; 

ben kullanmaya çalışıyorum:

System.Encoding.Default.GetBytes(stringFromFlatFile) 

ama sonuç dosyası bir Windows üzerinde görülemez

mevcut kod basitçe kullanarak bir bayt dizisi sütunu dökme makine (Hatalı hasarlı/bozuk veya çok büyük)

Kodlama türlerinden her birini System.Encoding (Ascii, UTFX, vb.) altında şanssız olarak denedim.

BCP queryout'da ek bir adım var varbinary(max) sütununda bayt dizisini korumak için almam gerekiyor mu? SqlDataReader'un .net uygulamasına eklemem gereken bayt dizisini döndürmek için özel bir özelliği var mı?

+1

Binary jpg dosyasını dosyaya koyan koda bakın. Bu size bir ipucu verebilir. BCP sorgulaması, baytların bir dize temsili oluşturuyor mu. Yani, sayısal değer 65 96 71 bayt yerine bir "0x659671" dizesi olurdu. Ayrıca o alandan mevcut kod okumaya sahipseniz ona bakın. –

+0

Verilerin masaya nasıl koyulduğuna erişemiyorum, ancak doğru yolda olduğunuzu kabul ediyorum. Görüntüyü varbin alanından bir bytrayray içine çekmek için bir veri okuyucu kullanan bir test yöntemi oluşturdum. Daha sonra bir Sistem için bayt dizisinin bir bellek akışını kullandım. Daha sonra jpg olarak kaydetmek için System.Drawing.Image kullandım. Görüntü pencerelerde görüntülenebilir. Daha sonra bayt dizisinden bir dize oluşturmak için kullanılabilir her kodlama yöntemini kullandım. Hiçbiri bcp tarafından oluşturulan dizgeye benzemiyor. – Roger

+0

Bcp, "FFD8FFE000104A46494600010100000100010000FFDB00430 ...." gibi görünen bir dize oluşturur. – Roger

cevap

1

Bu durumu, size özel durumunuzda bir çözüme götürecek bir referans olarak gönderiyorum.

Bu benim için 8 yıl boyunca 24x7x365 çalışmıştır.

Dize dönüştürme veya kodlamanın hiçbir zaman kullanılmadığını belirtmek istiyorum. Baytlar her zaman bayt olarak kalır.

Bu

VB.Net kodunu olduğunu

' The following code has run untouched for 6 years and gets images for me from the DB 
' You may be able to see a detail that is relavent to you 
' NOTE The field binary read is by numeric field number - not by field name string 
'  Notice the the buffer is grabbed in chunks into the buffer - not all at once 
'  Notice the CommandBehavior.SequentialAccess modifier 
' 
' There is no "conversion" of bytes thorugh any casting going on with encoding 
' The bytes are left untouched 
' 
' At the end is the snippit I use to write out the file to disk 


Dim filledByteArray As Byte() 
    filledByteArray = Nothing 

Dim cn As New SqlConnection 
Dim cmd As New SqlCommand 
Dim drNewPdf As SqlDataReader 

      cn.ConnectionString = "MyConnectionStringHere" 

      cmd.Connection = cn 
      cmd.CommandText = "myStoredProcWithOneOftheFieldsIsBinary" 
      cmd.CommandType = CommandType.StoredProcedure 

      ' notice the command behavior argument 
      cn.Open() 
      drNewPdf = cmd.ExecuteReader(CommandBehavior.SequentialAccess) 

      If drNewPdf.HasRows Then 

       While drNewPdf.Read 

      imgIdInt = drNewPdf.Item("FldNameOfInternalIntegerIdField") 

        ' - - - - - - - - - - - - - - - - - - - 
        ' Read the Binary Field Here 
        ' - - - - - - - - - - - - - - - - - - - 


        Dim ms As MemoryStream     ' 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. 



        ' Create a stream to hold the output. 
        ms = New MemoryStream() 
        bw = New BinaryWriter(ms) 

        ' Reset the starting byte for a new BLOB. 
        startIndex = 0 

        ' READ THE FIELD HERE !!!! And Below in the loop 
        '5 '"ImageFieldBLOB" 

        ' Read bytes into outbyte() and retain the number of bytes returned. 
        retval = drNewPdf.GetBytes(5, 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 

         ' READ THE FIELD HERE !!!! And Above in the loop 
         '5 is the field index into the recordset - use field id 5 in this case 
         ' Read bytes into outbyte() and retain the number of bytes returned. 
         retval = drNewPdf.GetBytes(5, startIndex, outbyte, 0, bufferSize) 
        Loop 

        ' Write the remaining buffer. 
        bw.Write(outbyte, System.Convert.ToInt32(0L), System.Convert.ToInt32(retval)) 
        bw.Flush() 


        streamLEN = System.Convert.ToInt32(ms.Length) 

        filledByteArray = New Byte(streamLEN - 1) {} 
        ms.Seek(0, SeekOrigin.Begin) 
        ms.Read(filledByteArray, 0, streamLEN) 


        ' Close the output file. 
        bw.Close() 
        ms.Close() 


        ' - - - - - - - - - - - - - - - - - - - 
        ' End the Binary Field Read - Loop to next record - 
        ' - - - - - - - - - - - - - - - - - - - 
       End While 
      Else 
       'No Rows ? 

      End If 


      cn.Close() 
      cn.Dispose() 





' A COMPLETELY DIFFERENT CHUNK OF CODE FOR PHYSICALLY WRITING TO DISK 
      Dim outFileStream As FileStream 

      outFileStream = New FileStream(selectedFileInfo.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite) 

      outFileStream.Write(byteArrayFromDb, 0, byteArrayFromDb.Length) 

      outFileStream.Flush() 

      outFileStream.Close() 

Benim asıl nokta bayt bir dize dönüştürme asla olmasıdır.

+0

Gönderdiğiniz için teşekkür ederim, ama zaten C# ile çalışan benzer bir şey var. Umudum, süreci hızlandırmak için bcp kullanmaktı, ideal bir yaklaşım olarak ortaya çıkmayabilir. – Roger