我使用Oracle存储照片。您需要使用一个out参数来检索照片,该参数的类型与照片存储在的字段的类型相同。我想这是一个斑点。在将照片存储到数据库中之前,必须将其转换为字节数组。由于这不是问题所在,我将发布用于从Oracle数据库中检索照片的代码,转换为Sql Server代码应该很简单。
我打开与数据库的连接,并将其传递给检索函数。我返回一个数据集,其中包含一行数据、照片和其他识别信息。然后我将BLOB分配给一个变量,从中执行您需要的操作。我所做的是将其分配给一个图像控件,该控件调用另一个aspx页面来检索照片(imageURL)。检索它的代码位于名为“Image.aspx”的文件中,该文件随后将照片返回给图像控件。html位于GetPhoto函数下面。这样我就不需要将照片存储在硬盘上的任何位置并检索它。
Dim MyConnection As New OracleConnection
Dim MyDataSet As New DataSet
Dim MyPhoto() As Byte = {}
MyConnection = OpenConnection(Session("USERNAME"), Session("PASSWORD"))
MyDataSet = GetPhoto(MyConnection, pPhotoID)
myPhoto = MyDataSet.Tables("Data").Rows(0)("Photo")
Public Function GetPhoto(ByVal TheConnection As OracleConnection, ByVal pPhotoID As String) As DataSet
Dim myCommand As New OracleCommand
Dim DS As New DataSet
Try
With myCommand
.Connection = TheConnection
.CommandText = "Darlington.PlayerSignUps.GetPhoto"
.CommandType = CommandType.StoredProcedure
.Parameters.Clear()
.Parameters.Add(New OracleParameter("pPhotoID", OracleDbType.Varchar2, pPlayerID.Length)).Value = pPhotoID
.Parameters.Add(New OracleParameter("pDataOut", OracleDbType.RefCursor))
.Parameters(0).Direction = ParameterDirection.Input
.Parameters(1).Direction = ParameterDirection.Output
Dim DA As New OracleDataAdapter(myCommand)
DA.Fill(DS, "DATA")
GetPhoto = DS
End With
Catch exc As Exception
Throw New Exception("Error occured while retreiving photo from database, the error is: " & exc.Message)
End Try
myCommand = Nothing
End Function
<tr>
<td style="width: 372px; text-align: center">
<asp:Image ID="Image_Photo" runat="server" ImageUrl="Image.aspx" />
</td>
</tr>