ファイルからの画像の挿入 (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 テーブルに従業員情報を追加するコード サンプルを次に示します。 従業員の写真がファイルから読み取られ、テーブルの 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;
}
参照
概念
SQL Server データ型のマッピング (ADO.NET)