Aracılığıyla paylaş


İkili Verileri Alma

Varsayılan olarak, DataReader tüm veri satırı kullanılabilir olduğunda gelen verileri satır olarak yükler. Ancak ikili büyük nesnelerin (BLOB'lar) farklı bir işleme ihtiyacı vardır, çünkü bunlar tek bir satırda içerilemeyen gigabaytlar kadar veri içerebilir. Command.ExecuteReader yönteminin, CommandBehavior argümanı alarak DataReader'ın varsayılan davranışını değiştirecek bir aşırı yüklemesi vardır. SequentialAccess öğesini ExecuteReader yöntemine, DataReader'nin varsayılan davranışını değiştirmek için geçirebilirsiniz. Bu, veri satırlarını yüklemek yerine, verilerin alındıkça sırayla yüklenmesini sağlar. Bu, BLOB'ları veya diğer büyük veri yapılarını yüklemek için idealdir. Bu davranışın veri kaynağınıza bağlı olabileceğini unutmayın. Örneğin, Microsoft Access'ten bir BLOB döndürülmesi, verinin alındığı sırada bölümler hâlinde yüklenmek yerine, BLOB'un tamamının belleğe yüklenmesine neden olur.

DataReader kullanacak şekilde ayarlarken, döndürülen alanlara hangi sırayla eriştiğinizi not etmek önemlidir. Kullanılabilir olduğunda satırın tamamını yükleyen DataReader'ın varsayılan davranışı, bir sonraki satır okunana kadar herhangi bir sırada döndürülen alanlara erişmenizi sağlar. Ancak SequentialAccess kullanırken, DataReader tarafından döndürülen alanlara sırasıyla erişmeniz gerekir. Örneğin, sorgunuz üç sütun döndürürse ve üçüncüsü BLOB ise, üçüncü alandaki BLOB verilerine erişmeden önce birinci ve ikinci alanların değerlerini döndürmeniz gerekir. Birinci veya ikinci alanlardan önceki üçüncü alana erişirseniz, birinci ve ikinci alan değerleri artık kullanılamaz. Bunun nedeni, SequentialAccess öğesinin DataReader'i sıralı olarak veri döndürecek şekilde değiştirmiş olması ve DataReader öğrencisinden sonra verinin artık mevcut olmamasıdır.

BLOB alanındaki verilere erişirken verileri bir diziye doldurmak için GetBytes veya GetChars, DataReader'ın yazılı erişimcilerini kullanın. Ancak karakter verileri için de kullanabilirsiniz GetString . sistem kaynaklarını korumak için blob değerinin tamamını tek bir dize değişkenine yüklemek istemeyebilirsiniz. Bunun yerine, döndürülecek verilerin belirli bir arabellek boyutunu ve döndürülen verilerden okunacak ilk bayt veya karakterin başlangıç konumunu belirtebilirsiniz. GetBytes ve GetChars döndürülen bayt veya karakter sayısını temsil eden bir long değer döndürür. veya GetBytes null bir dizi geçirirseniz, döndürülen uzun değer BLOB'daki toplam bayt veya karakter sayısı olur. İsteğe bağlı olarak, dizideki bir dizini okunacak veriler için başlangıç konumu olarak belirtebilirsiniz.

Örnek

Aşağıdaki örnek, Microsoft SQL Server'daki örnek veritabanından pubs yayımcı kimliğini ve logosunu döndürür. Yayımcı kimliği (pub_id) bir karakter alanıdır ve logo da BLOB olan bir resimdir. logo Alan bir bit eşlem olduğundan, örnek GetBytes kullanarak ikili verileri döndürür. Alanlara sırayla erişilmesi gerektiğinden, logodan önce mevcut veri satırındaki yayımcı kimliğine erişildiğine dikkat edin.

' Assumes that connection is a valid SqlConnection object.
Dim command As SqlCommand = New SqlCommand( _
  "SELECT pub_id, logo FROM pub_info", connection)

' Writes the BLOB to a file (*.bmp).
Dim stream As FileStream
' Streams the binary data to the FileStream object.
Dim writer As BinaryWriter
' The size of the BLOB buffer.
Dim bufferSize As Integer = 100
' The BLOB byte() buffer to be filled by GetBytes.
Dim outByte(bufferSize - 1) As Byte
' The bytes returned from GetBytes.
Dim retval As Long
' The starting position in the BLOB output.
Dim startIndex As Long = 0

' The publisher id to use in the file name.
Dim pubID As String = ""

' Open the connection and read data into the DataReader.
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader(CommandBehavior.SequentialAccess)

Do While reader.Read()
  ' Get the publisher id, which must occur before getting the logo.
  pubID = reader.GetString(0)

  ' Create a file to hold the output.
  stream = New FileStream( _
    "logo" & pubID & ".bmp", FileMode.OpenOrCreate, FileAccess.Write)
  writer = New BinaryWriter(stream)

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

  ' Read bytes into outByte() and retain the number of bytes returned.
  retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize)

  ' Continue while there are bytes beyond the size of the buffer.
  Do While retval = bufferSize
    writer.Write(outByte)
    writer.Flush()

    ' Reposition start index to end of the last buffer and fill buffer.
    startIndex += bufferSize
    retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize)
  Loop

  ' Write the remaining buffer.
  writer.Write(outByte, 0 , retval - 1)
  writer.Flush()

  ' Close the output file.
  writer.Close()
  stream.Close()
Loop

' Close the reader and the connection.
reader.Close()
connection.Close()
// Assumes that connection is a valid SqlConnection object.
SqlCommand command = new SqlCommand(
  "SELECT pub_id, logo FROM pub_info", connection);

// Writes the BLOB to a file (*.bmp).
FileStream stream;
// Streams the BLOB to the FileStream object.
BinaryWriter writer;

// Size of the BLOB buffer.
int bufferSize = 100;
// The BLOB byte[] buffer to be filled by GetBytes.
byte[] outByte = new byte[bufferSize];
// The bytes returned from GetBytes.
long retval;
// The starting position in the BLOB output.
long startIndex = 0;

// The publisher id to use in the file name.
string pubID = "";

// Open the connection and read data into the DataReader.
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);

while (reader.Read())
{
  // Get the publisher id, which must occur before getting the logo.
  pubID = reader.GetString(0);

  // Create a file to hold the output.
  stream = new FileStream(
    "logo" + pubID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
  writer = new BinaryWriter(stream);

  // Reset the starting byte for the new BLOB.
  startIndex = 0;

  // Read bytes into outByte[] and retain the number of bytes returned.
  retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);

  // Continue while there are bytes beyond the size of the buffer.
  while (retval == bufferSize)
  {
    writer.Write(outByte);
    writer.Flush();

    // Reposition start index to end of last buffer and fill buffer.
    startIndex += bufferSize;
    retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
  }

  // Write the remaining buffer.
  writer.Write(outByte, 0, (int)retval);
  writer.Flush();

  // Close the output file.
  writer.Close();
  stream.Close();
}

// Close the reader and the connection.
reader.Close();
connection.Close();

Ayrıca bakınız