所以我有一个带有1个表(记录)的MS Access数据库和2个字段(" RecordID"(Number),这是主键," LowRes"( OLE Object)这是一个低分辨率图像)。大约有100条记录。
我尝试将Access表加载到VB.net中的DataTable(ID_Table)中。
到目前为止代码:
Dim cnString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=SBS2257_ID.accdb;"
Dim theQuery As String = "SELECT [RecordID], [LowRes] FROM [Records];"
Using CN As New OleDbConnection(cnString)
Dim command As New OleDbCommand(theQuery, CN)
Using objDataAdapter = New OleDbDataAdapter(command)
Dim ID_Table As New DataTable
CN.Open()
Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
Dim picture As Image = Nothing
Using stream As New IO.MemoryStream(pictureData)
picture = Image.FromStream(stream)
objDataAdapter.Fill(ID_Table)
End Using
End Using
End Using
然而," DirectCast"当我告诉它在我的SQL语句中查看多于1个字段且数据类型不匹配时命令失败(如果我只是做[LowRes]它不会抛出错误)。但是,当我试图通过objDataAdapter将结果应用到表中时,我再次陷入困境,它没有用任何东西填充表格?我也注意到"图片"仅包含数据库中的第一个图像。
我可以使用" WHERE RECORDID = ..."将此数据库查询放在一个函数中。并手动循环建立表格返回"图片"每次,但我想避免运行100次函数,尤其是访问数据库的函数。
是否可以读取包含图像的整个数据库,然后直接将其直接加载到Datatable中?
编辑:所以我让这个工作:
Dim strConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=SBS2257_ID.accdb;"
Dim strSQL As String = "SELECT [RecordID], [LowRes] FROM [Records];"
Using objConnection = New OleDbConnection(strConnection)
Using objCommand = New OleDbCommand(strSQL, objConnection)
Using objDataAdapter = New OleDbDataAdapter(objCommand)
Dim objDataTable As New DataTable("IDs")
objDataAdapter.Fill(objDataTable)
Return objDataTable
End Using
End Using
End Using
我怎么去查看第0行,第1列应该是第一个通过.ToString的LowRes图像使用此代码:
Private Sub PrintValues(ByVal table As DataTable)
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
MsgBox(row(col).ToString())
Next col
Next row
End Sub
它只显示" System.Byte []"。它知道它的Byte数据类型,但是如何在图片框中显示它?
答案 0 :(得分:0)
ExecuteScalar()
执行查询,并返回查询返回的结果集中第一行的第一列。
,因为您的查询是
Dim theQuery As String = "SELECT [RecordID], [LowRes] FROM [Records];"
第一列是RecordID
,而不是Byte()
。
您可以按以下方式更改查询:
Dim theQuery As String = "SELECT [LowRes] FROM [Records];"
或者您必须使用其他方法从数据库中获取数据
Dim strSql As String = "SELECT [RecordID], [LowRes] FROM [Records]"
Dim dtb As New DataTable
Using cnn As New OleDbConnection(connectionString)
cnn.Open()
Using dad As New OleDbDataAdapter(strSql, cnn)
dad.Fill(dtb)
End Using
cnn.Close()
End Using