using System.Data.SqlClient; using System.IO; // Open a file stream to read binary data FileStream stream = new FileStream("data.bin", FileMode.Open); // Create a BinaryDataReader instance to read from the stream BinaryDataReader reader = new BinaryDataReader(stream); // Read a sequence of bytes from the stream byte[] buffer = new byte[1024]; int bytesRead = reader.Read(buffer, 0, buffer.Length); // Close the stream and reader reader.Close(); stream.Close();
using System.Data.SqlClient; // Create a byte array representing some binary data byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }; // Create a BinaryDataReader instance to read from the byte array BinaryDataReader reader = new BinaryDataReader(data); // Read the first byte from the array byte b = reader.ReadByte(); // Close the reader reader.Close();
using System.Data.SqlClient; // Create a connection string to access a SQL Server database string connectionString = "Server=localhost;Database=MyDatabase;Trusted_Connection=True;"; // Create a SQL command to execute a stored procedure that returns binary data string commandText = "EXEC dbo.GetBlobData"; SqlCommand command = new SqlCommand(commandText); command.CommandType = CommandType.StoredProcedure; command.Connection = new SqlConnection(connectionString); // Execute the command and get the result set BinaryDataReader reader = command.ExecuteReader(); // Read binary data from the result set while (reader.Read()) { byte[] data = (byte[])reader["BlobData"]; // process the binary data... } // Close the reader and command reader.Close(); command.Dispose();Package library: System.Data.SqlClient (included in the .NET Framework)