Aracılığıyla paylaş


Dosyadan görüntü ekleme

Veri kaynağınızdaki alanın türüne bağlı olarak, veritabanına ikili veya karakter verileri olarak ikili büyük nesne (BLOB) yazabilirsiniz. BLOB, genellikle belge ve resim içeren , textve ntext veri türlerine başvuran imagegenel bir terimdir.

Veritabanınıza bir BLOB değeri yazmak için uygun INSERT veya UPDATE deyimini yayınlayıp BLOB değerini giriş parametresi olarak geçirin (bkz . Parametreleri ve Parametre Veri Türlerini Yapılandırma). BLOB'unuz BIR SQL Server text alanı gibi bir metin olarak depolanıyorsa, BLOB'unu dize parametresi olarak geçirebilirsiniz. BLOB, SQL Server image alanı gibi ikili biçimde depolanıyorsa, türünde byte bir diziyi ikili parametre olarak geçirebilirsiniz.

Örnek

Aşağıdaki kod örneği, Northwind veritabanındaki Employees tablosuna çalışan bilgilerini ekler. Çalışanın fotoğrafı bir dosyadan okunur ve tablodaki Resim alanına (resim alanı) eklenir.

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;
}

Ayrıca bkz.