从vb.net中的访问数据库到图片框中获取图像数据 [英] fetch image data from access database to picture box in vb.net

查看:205
本文介绍了从vb.net中的访问数据库到图片框中获取图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉如何从数据库(访问)获取图像到

视觉基本图片框控件。我一直在使用的代码是

,但它给了我无效的参数错误....


Dim connection As New OleDb.OleDbConnection()

Dim cmd As新OleDb.OleDbCommand()

cmd.CommandText ="选择ID,名称,描述,图片来自

MASTER其中ID喜欢''' &安培;修剪(cmbID.Text)& "''"

cmd.Connection = connection

Dim myReader作为OleDb.OleDbDataReader

cmd.CommandType = CommandType.Text

openOLEDBconnection(SQL_CONNECTION_STRING,连接)

myReader =

cmd.ExecuteReader(CommandBehavior.CloseConnection)

myReader.Read ()

txtId.Text = myReader(0)

txtName.Text = myReader(1).ToString()

txtDescription.Text = myReader(2).ToString()

如果不是IsDBNull(myReader(3))那么

试试

试试

Dim a(myReader.GetBytes(3,0,Nothing,0,

Integer.MaxValue) - 1)By Byte

myReader.GetBytes(3,0, a,0,a.Length)

Dim b As New MemoryStream(a)

with pbImage

.Image = image.FromStream(b )

.SizeMode =

PictureBoxSizeMode.CenterImage

.BorderStyle = BorderStyle.Fixed3D



Catch exp As Exception

MsgBox(exp.Message)

结束尝试

Catch exp As System.InvalidCastException

MsgBox(exp.Message)

结束尝试

结束如果

结束时

myReader.Close()

can anybody tell how to fetch image from database(access) to the
visual basic picture box control. the code i have been using is as
below but it gives me invalid paramter error....

Dim connection As New OleDb.OleDbConnection()
Dim cmd As New OleDb.OleDbCommand()
cmd.CommandText = "select ID,Name,Description,Image from
MASTER where ID like ''" & Trim(cmbID.Text) & "''"
cmd.Connection = connection
Dim myReader As OleDb.OleDbDataReader
cmd.CommandType = CommandType.Text
openOLEDBconnection(SQL_CONNECTION_STRING, connection)
myReader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read()
txtId.Text = myReader(0)
txtName.Text = myReader(1).ToString()
txtDescription.Text = myReader(2).ToString()
If Not IsDBNull(myReader(3)) Then
Try
Try
Dim a(myReader.GetBytes(3, 0, Nothing, 0,
Integer.MaxValue) - 1) As Byte
myReader.GetBytes(3, 0, a, 0, a.Length)
Dim b As New MemoryStream(a)
With pbImage
.Image = image.FromStream(b)
.SizeMode =
PictureBoxSizeMode.CenterImage
.BorderStyle = BorderStyle.Fixed3D
End With
Catch exp As Exception
MsgBox(exp.Message)
End Try
Catch exp As System.InvalidCastException
MsgBox(exp.Message)
End Try
End If
End While
myReader.Close()

推荐答案

嗨Abhisek,


它主要与

对象中访问文件中的图像封闭有关。


我曾经复制了一些代码,并尝试粘贴你的代码,给它一个

试试?

(你自己要把它设置在尝试再次阻止)


我没有测试它,我只是粘贴它,所以请告诉它是否确实有效,而不是

我也知道吗?


Cor

\\\

昏暗连接为新OleDb.OleDbConnection()

Dim cmd As New OleDb.OleDbCommand()

cmd.CommandText ="选择ID,名称,描述,图片来自

MASTER其中ID类似''" &安培;修剪(cmbID.Text)& "''"

connection.open

Dim dr As OleDbDataReader =

cmd.ExecuteReader(CommandBehavior.SequentialAccess)

dr.Read()

Dim FieldLen As Integer = dr.Item(1).ToString.Length

Dim a(FieldLen - 1)As Byte

Dim startIndex As Integer = 0

dim RetVal as long = dr.GetBytes(1,startIndex,a,0,a.Length)

dr.Close()

connection.Close()

connection.dispose

Dim b As New MemoryStream(a)
Hi Abhisek,

It has mostly to do with the enclosing from a image in an access file in an
object.

I once copied some code and have tried that to paste in your code, give it a
try?
(You have yourself to set it right in the try block again)

I did not test it, I just pasted it in, so please tell if it did work, than
I know that also?

Cor
\\\
Dim connection As New OleDb.OleDbConnection()
Dim cmd As New OleDb.OleDbCommand()
cmd.CommandText = "select ID,Name,Description,Image from
MASTER where ID like ''" & Trim(cmbID.Text) & "''"
connection.open
Dim dr As OleDbDataReader =
cmd.ExecuteReader(CommandBehavior.SequentialAccess )
dr.Read()
Dim FieldLen As Integer = dr.Item(1).ToString.Length
Dim a(FieldLen - 1) As Byte
Dim startIndex As Integer = 0
dim RetVal as long = dr.GetBytes(1, startIndex, a, 0, a.Length)
dr.Close()
connection.Close()
connection.dispose
Dim b As New MemoryStream(a)
使用pbImage
.Image = image.FromStream(b)
.SizeMode =
PictureBoxSizeMode.CenterImage
.BorderStyle = BorderStyle.Fixed3D
捕获exp作为异常
MsgBox(exp.Message)
结束尝试
Catch exp As System.InvalidCastException
MsgBox(exp.Message)
结束尝试
结束如果
结束时
With pbImage
.Image = image.FromStream(b)
.SizeMode =
PictureBoxSizeMode.CenterImage
.BorderStyle = BorderStyle.Fixed3D
End With
Catch exp As Exception
MsgBox(exp.Message)
End Try
Catch exp As System.InvalidCastException
MsgBox(exp.Message)
End Try
End If
End While



不,它也不是那样工作。

i也试过下面给出的微软网站上给出的代码,但是

也给了我同样的错误。我错过了其他的东西。请帮助


Dim da As New OleDb.OleDbDataAdapter(cmd)

Dim ds As New DataSet()

da.Fill (ds,MASTER)

Dim c As Integer = ds.Tables(" MASTER")。Rows.Count

如果c> 0然后

Dim bytBLOBData()As Byte = _

ds.Tables(" MASTER")。行(c - 1)(Image)

Dim stmBLOBData As New MemoryStream(bytBLOBData)

pbImage.Image = Image.FromStream(stmBLOBData)

结束如果
no it is not working that way also.
i have also tried the code given on microsoft site given below but it
also gives me the same error. am i missing somthing else. please help

Dim da As New OleDb.OleDbDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "MASTER")
Dim c As Integer = ds.Tables("MASTER").Rows.Count
If c > 0 Then
Dim bytBLOBData() As Byte = _
ds.Tables("MASTER").Rows(c - 1)("Image")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
pbImage.Image = Image.FromStream(stmBLOBData)
end if


您好Abhishek,


此代码适用于未嵌入ole对象的标准blob。

我不知道你是否有这个,但你可以尝试改变这一点。最好总是使用Option Strict On,比这种问题(当

就是这样)显示更早


Ctype(ds.Tables(" MASTER")。行(c - 1)(图像),字节())


我希望这有助于吗?


Cor

Hi Abhishek,

This code is for a standard blob not embeded in an ole object.
I do not know if you have that, however you can try to change this. It is
also better to always use Option Strict On, than this kind of problems (when
it is that) shows earlier

Ctype(ds.Tables("MASTER").Rows(c - 1)("Image"),Byte())

I hope this Helps?

Cor


这篇关于从vb.net中的访问数据库到图片框中获取图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆