Een afbeelding uit een bestand invoegen

U kunt een binair groot object (BLOB) schrijven naar een database als binaire of tekengegevens, afhankelijk van het type veld in uw gegevensbron. BLOB is een algemene term die verwijst naar de text, ntexten image gegevenstypen, die meestal documenten en afbeeldingen bevatten.

Als u een BLOB-waarde naar uw database wilt schrijven, geeft u de juiste INSERT- of UPDATE-instructie uit en geeft u de BLOB-waarde door als invoerparameter (zie Parameters en parametergegevenstypen configureren). Als uw BLOB is opgeslagen als tekst, zoals een SQL Server-veld text , kunt u de BLOB doorgeven als een tekenreeksparameter. Als de BLOB is opgeslagen in binaire indeling, zoals een SQL Server-veld image , kunt u een matrix van het type byte doorgeven als een binaire parameter.

Voorbeeld

In het volgende codevoorbeeld worden werknemersgegevens toegevoegd aan de tabel Werknemers in de Northwind-database. Een foto van de werknemer wordt gelezen uit een bestand en toegevoegd aan het veld Foto in de tabel, een afbeeldingsveld.

Public Shared Sub AddEmployee( _
  lastName As String, _
  firstName As String, _
  title As String, _
  hireDate As DateTime, _
  reportsTo As Integer, _
  photoFilePath As String, _
  connectionString As String)

  Dim photo() as Byte = GetPhoto(photoFilePath)

  Using connection As SqlConnection = New SqlConnection( _
    connectionString)

  Dim command As SqlCommand = New SqlCommand( _
    "INSERT INTO Employees (LastName, FirstName, Title, " & _
    "HireDate, ReportsTo, Photo) " & _
    "Values(@LastName, @FirstName, @Title, " & _
    "@HireDate, @ReportsTo, @Photo)", connection)

  command.Parameters.Add("@LastName",  _
    SqlDbType.NVarChar, 20).Value = lastName
  command.Parameters.Add("@FirstName", _
    SqlDbType.NVarChar, 10).Value = firstName
  command.Parameters.Add("@Title", _
    SqlDbType.NVarChar, 30).Value = title
  command.Parameters.Add("@HireDate", _
    SqlDbType.DateTime).Value = hireDate
  command.Parameters.Add("@ReportsTo", _
    SqlDbType.Int).Value = reportsTo

  command.Parameters.Add("@Photo", _
    SqlDbType.Image, photo.Length).Value = photo

  connection.Open()
  command.ExecuteNonQuery()

  End Using
End Sub

Public Shared Function GetPhoto(filePath As String) As Byte()
  Dim stream As FileStream = new FileStream( _
     filePath, FileMode.Open, FileAccess.Read)
  Dim reader As BinaryReader = new BinaryReader(stream)

  Dim photo() As Byte = reader.ReadBytes(stream.Length)

  reader.Close()
  stream.Close()

  Return photo
End Function
public static void AddEmployee(
  string lastName,
  string firstName,
  string title,
  DateTime hireDate,
  int reportsTo,
  string photoFilePath,
  string connectionString)
{
  byte[] photo = GetPhoto(photoFilePath);

  using (SqlConnection connection = new SqlConnection(
    connectionString))

  SqlCommand command = new SqlCommand(
    "INSERT INTO Employees (LastName, FirstName, " +
    "Title, HireDate, ReportsTo, Photo) " +
    "Values(@LastName, @FirstName, @Title, " +
    "@HireDate, @ReportsTo, @Photo)", connection);

  command.Parameters.Add("@LastName",
     SqlDbType.NVarChar, 20).Value = lastName;
  command.Parameters.Add("@FirstName",
      SqlDbType.NVarChar, 10).Value = firstName;
  command.Parameters.Add("@Title",
      SqlDbType.NVarChar, 30).Value = title;
  command.Parameters.Add("@HireDate",
       SqlDbType.DateTime).Value = hireDate;
  command.Parameters.Add("@ReportsTo",
      SqlDbType.Int).Value = reportsTo;

  command.Parameters.Add("@Photo",
      SqlDbType.Image, photo.Length).Value = photo;

  connection.Open();
  command.ExecuteNonQuery();
  }
}

public static byte[] GetPhoto(string filePath)
{
  FileStream stream = new FileStream(
      filePath, FileMode.Open, FileAccess.Read);
  BinaryReader reader = new BinaryReader(stream);

  byte[] photo = reader.ReadBytes((int)stream.Length);

  reader.Close();
  stream.Close();

  return photo;
}

Zie ook