让我们有一个包含图像的表格(而不是您可以使用所需大小的最大值):
CREATE TABLE dbo.STORED_IMAGES
(
ID int NOT NULL IDENTITY (1, 1),
Img varbinary(MAX) NULL
)
存储图像:
Dim CMD As New SqlClient.SqlCommand("insert into STORED_IMAGES(Img) values(@Img)",cn)
CMD.Parameters.Add("@Img", SqlDbType.VarBinary, Integer.MaxValue).Value = ReadFile(Path)
读取文件功能:
Private Function ReadFile(sPath As String) As Byte()
Dim data As Byte() = Nothing
Dim fInfo As New IO.FileInfo(sPath)
Dim numBytes As Long = fInfo.Length
Using fStream As New IO.FileStream(sPath, IO.FileMode.Open, IO.FileAccess.Read)
Dim br As New IO.BinaryReader(fStream)
data = br.ReadBytes(CInt(numBytes))
End Using
Return data
End Function
从数据库获取文件:
Dim fcmd As New SqlCommand("select Img from STORED_IMAGES where ID=@ID", cn)
fcmd.Parameters.Add("@ID", SqlDbType.Int).Value = 3 'e.g.
Dim bytes As Byte() = fcmd.ExecuteScalar
我想使用表格适配器,您的数据表将包含字节()类型的列。