如何从SQL Server检索图片框图像到VB.NET [英] How to retrieve picturebox image to VB.NET from SQL server

查看:88
本文介绍了如何从SQL Server检索图片框图像到VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理vb.net中的picturebox检索。如果我运行一个程序,它会显示一条错误消息,说无法将类型为'System.Byte []'的对象强制转换为'System.Drawing.Image 。请帮我解决这个问题。



我的尝试:



Private Sub view_Click(发送者作为对象,e作为EventArgs)处理Button3.Click

Dim conn As New SqlConnection

Dim mycommand As SqlCommand = New SqlCommand()

Dim datareader As SqlDataReader = Nothing

conn.ConnectionString =Data Source = HP\SQLEXPRESS; Initial Catalog = CRIME; Integrated Security = True

conn.Open()

Dim query As String

query =select [DATE OF ARREST],[犯罪名称],[刑事地址], [刑事犯罪],[刑事责任描述],[刑事责任],[刑事诉讼],[刑事照片],刑事[打印]在哪里[刑事报告号码] ='& TextBox1.Text& '

mycommand = New SqlCommand(query,conn)

datareader = mycommand.ExecuteReader()

while datareader.Read

如果datareader IsNot Nothing那么

DateTimePicker1.Value = datareader.Item(DATE OF ARREST)

TextBox2.Text = datareader.Item(NAME OF CRIMINAL)

TextBox3.Text = datareader.Item(刑事地址)

TextBox4.Text = datareader.Item(犯罪嫌疑人)

TextBox5.Text = datareader.Item(犯罪描述)

TextBox6.Text = datareader.Item(犯罪的诅咒)

TextBox7.Text = datareader.Item(对抗犯罪行为)

PictureBox1.Image = datareader.Item(犯罪照片)

PictureBox2.Image = datareader。项目(FINGER PRINT)

结束如果

结束时

conn.Close()

结束Sub

Hi,I am dealing with picturebox retrieval in vb.net.If i run a program it displays a error message saying that "Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image".Please help me sorting out this problem.

What I have tried:

Private Sub view_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim conn As New SqlConnection
Dim mycommand As SqlCommand = New SqlCommand()
Dim datareader As SqlDataReader = Nothing
conn.ConnectionString = "Data Source=HP\SQLEXPRESS;Initial Catalog=CRIME;Integrated Security=True"
conn.Open()
Dim query As String
query = "select [DATE OF ARREST],[NAME OF CRIMINAL],[ADDRESS OF CRIMINAL],[OFFENCE OF CRIMINAL],[DESCRIPTION OF CRIMINAL],[CONFESSION OF CRIMINAL],[ACTION AGAINST CRIMINAL],[PHOTO OF CRIMINAL],[FINGER PRINT] FROM CRIMINAL WHERE [CRIMINAL REPORT NUMBER]='" & TextBox1.Text & "'"
mycommand = New SqlCommand(query, conn)
datareader = mycommand.ExecuteReader()
While datareader.Read
If datareader IsNot Nothing Then
DateTimePicker1.Value = datareader.Item("DATE OF ARREST")
TextBox2.Text = datareader.Item("NAME OF CRIMINAL")
TextBox3.Text = datareader.Item("ADDRESS OF CRIMINAL")
TextBox4.Text = datareader.Item("OFFENCE OF CRIMINAL")
TextBox5.Text = datareader.Item("DESCRIPTION OF CRIMINAL")
TextBox6.Text = datareader.Item("CONFESSION OF CRIMINAL")
TextBox7.Text = datareader.Item("ACTION AGAINST CRIMINAL")
PictureBox1.Image = datareader.Item("PHOTO OF CRIMINAL")
PictureBox2.Image = datareader.Item("FINGER PRINT")
End If
End While
conn.Close()
End Sub

推荐答案

你可以只是将一个字节数组转换为图像。

试试这个:

You can't just cast a byte array to an Image.
Try this:
Dim ms As New MemoryStream(arrayOfBytes)
Dim returnImage As Image = Image.FromStream(ms)





但是......不要那样做DB访问!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:



But ... don't do DB access like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



如果这是你用来输入图像到DB的代码的典型代码,它们将是无用。请参阅此处:为什么我会得到一个参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 代码在C#中,但很明显。

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And if that is typical of the code you used to enter the images to the DB, they are going to be useless. See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - the code is in C#, but it's pretty obvious.


这篇关于如何从SQL Server检索图片框图像到VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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