如何使用VB.NET中的datareader从acces数据库中检索图像 [英] How to retreive image from acces database using datareader in VB.NET

查看:69
本文介绍了如何使用VB.NET中的datareader从acces数据库中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将图像存储到访问数据库中,现在我想通过数据读取器检索图像请帮帮我。



[删除重复代码]



我的尝试:



 尝试 

Dim ms As System.IO.MemoryStream
Dim bmpImage As 位图(stphoto.Image)

bmpImage.Save(ms,System.Drawing.Imaging。 ImageFormat.Jpeg)
bytImage = ms.ToArray()
ms.Close()

Dim con 作为 OleDb.OleDbConnection
Dim cmd 作为 O leDb.OleDbCommand
Dim sqlstring As String
Dim 查询作为 String
sqlstring = Provider = Microsoft.ACE.OLEDB.12.0; Data Source = bin \ fee.accdb
con = OleDb.OleDbConnection(sqlstring)
con.Open()
query = INSERT INTO smaster([sid],[sname],[fname],[add],[dob],[ class],[college],[session],[cnumber],[gnumber],[photo])值(& sidtxt.Text& ,'& snametxt.Text& ','& fnametxt.Text& ','& addtxt.Text& ','& dobpick.Value.ToShortDateString& ','& classcombo.Text& ','& collegecombo.Text& ','& sessioncombo.Text& ','& mobtxt.Text& ','& gnumbertxt.Text& ','@ bytImage')

cmd = OleDb.OleDbCommand(query,con)
cmd.ExecuteNonQuery()

studentid = sidtxt.Text
studentname = snametxt。文本
' 打开添加主题表单的代码

Dim 结果作为 整数 = MessageBox.Show(< span class =code-string> 保存主数据,请单击确定添加主题 学生数据,MessageBoxButtons.OK)
如果 result = DialogResult。确定然后
addsub.Show()
结束 如果
Catch ex As Exception
MessageBox.Show(ex.Message)
End 尝试

解决方案

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



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

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

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

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

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

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

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

   -   ' 

其他一切都是评论。

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



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



如果你使用字符串连接来插入你的图像,那么你还有另外一个问题。它们都是垃圾,你无法取回它们......

见这里:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 代码全部都在C#中,但它非常简单,如果你无法应对,在线转换可以改变它。 / BLOCKQUOTE>

I use following code for storing image into access database and now I want to retrieve image through data reader please help me.

[removed duplicate code]

What I have tried:

 Try

	Dim ms As New System.IO.MemoryStream
	Dim bmpImage As New Bitmap(stphoto.Image)

	bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
	bytImage = ms.ToArray()
	ms.Close()

	Dim con As New OleDb.OleDbConnection
	Dim cmd As New OleDb.OleDbCommand
	Dim sqlstring As String
	Dim query As String
	sqlstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bin\fee.accdb"
	con = New OleDb.OleDbConnection(sqlstring)
	con.Open()
	query = "INSERT INTO smaster ([sid],[sname],[fname],[add],[dob],[class],[college],[session],[cnumber],[gnumber],[photo]) Values (" & sidtxt.Text & ",'" & snametxt.Text & "','" & fnametxt.Text & "','" & addtxt.Text & "','" & dobpick.Value.ToShortDateString & "','" & classcombo.Text & "', '" & collegecombo.Text & "','" & sessioncombo.Text & "','" & mobtxt.Text & "','" & gnumbertxt.Text & "','@bytImage')"

	cmd = New OleDb.OleDbCommand(query, con)
	cmd.ExecuteNonQuery()

	studentid = sidtxt.Text
	studentname = snametxt.Text
	'code for opening add subject form

	Dim result As Integer = MessageBox.Show("Master Data Saved, Please Click OK for adding Subjects", "Student Data", MessageBoxButtons.OK)
	If result = DialogResult.OK Then
		addsub.Show()
	End If
Catch ex As Exception
	MessageBox.Show(ex.Message)
End Try

解决方案

Don't do it 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'

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;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

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 you have used string concatenation to insert your images, well, you have another problem. They are all garbage, and you can't retrieve them...
See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - the code is all in C#, but it's pretty simple and online convertes can change it if you can't cope.


这篇关于如何使用VB.NET中的datareader从acces数据库中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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