使用 VB.NET 从 Access 中检索长二进制数据(图像)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21976958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve Long Binary Data (Image) from Access using VB.NET
提问by DidIReallyWriteThat
My code is
我的代码是
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source=" & My.Settings.bowlingballdatabase
Dim selectString As String = "Select Picture From BowlingBall WHERE LName = 'Smith'"
Dim oleConnect As New OleDb.OleDbConnection
oleConnect.ConnectionString = dbProvider & dbSource
oleConnect.Open()
Using oleDBCmd As OleDb.OleDbCommand = oleConnect.CreateCommand()
oleDBCmd.CommandType = CommandType.Text
oleDBCmd.CommandText = selectString
Using oleDbReader As OleDb.OleDbDataReader = oleDBCmd.ExecuteReader()
oleDbReader.Read()
Dim ImageBytes As Byte() = CType(oleDbReader(0), Byte())
Dim ms As New MemoryStream(ImageBytes)
Dim img As Image = Image.FromStream(ms)
Me.PictureBox1.Image = img
End Using
End Using
oleConnect.Close()
I think its a problem with the SQL commands maybe? I get the error "Parameter is not valid" on the following line:
我认为它可能是 SQL 命令的问题?我在以下行收到错误“参数无效”:
Dim img As Image = Image.FromStream(ms)
采纳答案by Gord Thompson
There is nothing wrong with your code per se. Your problem is almost certainly related to the way Access stores images when they are embedded from within the Access application itself. In those cases an OLE "wrapper" is added to the raw image data before it is stored in the table. This works okay when handling images from within Access, but it can cause problems when working with those images from external applications like your .NET project.
您的代码本身没有任何问题。您的问题几乎肯定与从 Access 应用程序本身嵌入图像时 Access 存储图像的方式有关。在这些情况下,在将原始图像数据存储到表中之前,会将 OLE“包装器”添加到原始图像数据中。这在处理来自 Access 中的图像时可以正常工作,但在处理来自外部应用程序(如 .NET 项目)的图像时可能会导致问题。
Your code retrieves the OLE-wrapped image object from the Access database and tries to create a .NET Imageobject with it. The problem is that while the Imageobject can recognize a variety of image types (e.g., bitmap, JPEG, etc.) the "OLE-wrapped image object format" is not one of them. Therefore you need to remove the OLE wrapper from the stream of bytes before passing it to Image.FromStream().
您的代码从 Access 数据库中检索 OLE 包装的图像对象,并尝试Image用它创建一个 .NET对象。问题是,虽然Image对象可以识别多种图像类型(例如,位图、JPEG 等),但“OLE 包装图像对象格式”不是其中之一。因此,您需要先从字节流中删除 OLE 包装器,然后再将其传递给Image.FromStream().
For more details see my other answer here, which shows how to "unwrap" objects using code from an earlier answer here.
回答by user2930100
when you do oleDbReader(0), you get the field name of the first field in the returned reader object. use oleDbReader(0)(0) to get the first item in the first column, you know how it goes.
当您执行 oleDbReader(0) 时,您将获得返回的阅读器对象中第一个字段的字段名称。使用 oleDbReader(0)(0) 获取第一列中的第一项,您知道它是怎么回事。
Dim ImageBytes As Byte() = CType(oleDbReader(0)(0), Byte())

