파일에서 이미지 삽입(ADO.NET)

데이터 소스의 필드 형식에 따라 BLOB(Binary Large Object)를 이진 또는 문자 데이터로 데이터베이스에 쓸 수 있습니다. BLOB는 주로 문서와 그림이 포함된 text, ntext 및 image 데이터 형식을 나타내는 일반적인 용어입니다.

데이터베이스에 BLOB 값을 쓰려면 적합한 INSERT 또는 UPDATE 문을 실행하고 BLOB 값을 입력 매개 변수(매개 변수 및 매개 변수 데이터 형식 구성(ADO.NET) 참조)로 전달합니다. BLOB를 SQL Server text 필드와 같이 텍스트로 저장하면 BLOB를 문자열 매개 변수로 전달할 수 있습니다. BLOB를 SQL Server image 필드와 같이 이진 형식으로 저장하면 byte 형식의 배열을 이진 매개 변수로 전달할 수 있습니다.

예제

다음 코드 예제에서는 Northwind 데이터베이스의 Employees 테이블에 직원 정보를 추가합니다. 파일에서 직원 사진을 읽어 테이블의 Photo 필드에 추가합니다. 이 필드는 이미지 필드입니다.

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

참고 항목

개념

큰 데이터 검색(ADO.NET)

SQL Server 데이터 형식 매핑(ADO.NET)

기타 리소스

명령을 사용하여 데이터 수정(ADO.NET)

SQL Server 이진 데이터 및 큰 값 데이터(ADO.NET)