【发布时间】:2014-01-29 21:53:58
【问题描述】:
使用:
- MS-Access 2013 SP1 x86
- MS-SQL Server 2012 SP1 CU8 x64
- 通过 ODBC DSN、SQL Server 驱动程序连接
- UpScene Database Workbench Pro v4.4.4 Pro for MS-SQL Server
My Access 2013 应用程序使用 SQL Server 2012 作为带有 ODBC 的后端数据库。我正在使用 VBA/ADO 向数据库读取/写入数据。
到目前为止,我一直未能从数据库中检索图像并将其分配给 Access 表单上的图像控件。图像存储在 SQL Server 表中(作为 VARBINARY(MAX) 字段。
在我将字段分配给 Image 控件时,它给出了运行时错误:“类型不匹配”。存储在数据库中的图像作为位图图像。我之前尝试过使用 Jpeg,但这是同样的错误。如何解决?
SQL Server 表定义和存储过程定义:
CREATE TABLE dbo.tbPhoto (
row_id Int IDENTITY NOT NULL,
student_id Int NOT NULL,
picture VarBinary(max),
date_updated DateTime NOT NULL,
date_created DateTime NOT NULL,
CONSTRAINT PK_tbPhoto PRIMARY KEY CLUSTERED (
row_id
)
)
获取图片的访问程序:
Private Sub load_studentdetails(AStudentID As Integer)
On Error GoTo errhnd
objStudent.GetStudentDetails AStudentID, StudentDetailsRS
Set Me.fmStudentReg_DtlA.Form.Recordset = StudentDetailsRS
' Me.fmStudentReg_DtlA.Form.Requery
objStudent.GetStudentPicture AStudentID, Me.fmStudentReg_DtlA!imgStudentPic
Exit Sub
errhnd:
MsgBox "Error: " & Err.Description
End Sub
Public Sub GetStudentPicture(AStudentID As Integer, ByRef APicture As Image)
On Error GoTo errhnd
Dim rs As New ADODB.Recordset
Set cmd.ActiveConnection = GetDBConnection
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "dbo.StudentPicture_S"
cmd.Parameters.Refresh
cmd(1) = AStudentID
rs.CursorLocation = adUseClient
rs.Open cmd
Set APicture = rs("picture") '<-----Raises the error: "Type mismatch"
Exit Sub
errhnd:
MsgBox "Error: " & Err.Description
End Sub
【问题讨论】:
标签: image ms-access ado recordset