从文件插入图像 (ADO.NET)

可以将二进制大对象 (BLOB) 作为二进制或字符数据写入数据库,具体视数据源的字段类型而定。 BLOB 是一个表示 text、ntext 和 image 数据类型的通用术语,这些数据类型通常包含文档和图片。

若要将 BLOB 值写入数据库,请发出相应的 INSERT 或 UPDATE 语句并传递 BLOB 值作为输入参数(请参见配置参数和参数数据类型 (ADO.NET))。 如果 BLOB 存储为文本格式(如 SQL Server text 字段),则可将 BLOB 作为字符串参数传递。 如果 BLOB 存储为二进制格式(如 SQL Server image 字段),则可将类型 byte 的数组作为二进制参数传递。

示例

下面的代码示例将雇员信息添加到 Northwind 数据库中的“Employees”表中。 从文件中读取雇员的照片并将其添加到表中的“照片”字段,此字段是一个图像字段。

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)