将VB应用程序中的图片发送到SQL数据库

问题描述:

我使用visual studio 2017来创建一个Visual Basic应用程序。我一直在尝试将图像上传到sql数据库,所有设置都正确,但不能让我的生活得到它的工作。整体代码的基础是用户上传成员照片,代码将更新所选成员的图像列。



我有什么尝试过:



我已尝试过本网站上的所有内容(在您链接到旧版之前/询问为什么我提交此内容)无济于事。 YouTube,谷歌,微软社区页面等,但不管我在数据库行/列中总是得到相同的结果:

Hi, Im using visual studio 2017 to create a Visual basic application. I have been trying to upload an image to an sql database, all set up correctly, but cant for the life of me get it to work. The basics of the overall code is that a user uploads a members photo, and the code will update the image column of a selected member.

What I have tried:

I have tried everything on this website (before you link to older ones/ask why im submitting this) to no avail. YouTube, google, Microsoft community pages etc but no matter what I always get the same result in the database row/column :

0x40696D67



我甚至已经去了几次我的代码,使用字节发送,将我的图像转换为程序内外的另一种文件格式,但仍然没有。

这是我一直在使用的代码,据我所知,它应该是没有问题的。


I have even gone and mutilated my code a couple of times, using bytes to send, converting my image to another file format both inside and outside of the program and still nothing.
Here is the code I have been using, as far as I can tell this is what it should be with no problems.

Try

               Dim connection As New SqlClient.SqlConnection(My.Settings.Memberinfo)
               Dim command As New SqlClient.SqlCommand("update " & My.Settings.currentaccessread & " set img='@img' where [First Name]= '" & TextBox2.Text & "' AND [LAST NAME]= '" & TextBox4.Text & "'", connection)

               Dim ms As New MemoryStream
               PictureBox1.BackgroundImage.Save(ms, pictureBox1.BackgroundImage.RawFormat)

               command.Parameters.Add("@img", SqlDbType.Image).Value = ms.ToArray()

               connection.Open()
               If command.ExecuteNonQuery() = 1 Then
                   MsgBox(TextBox2.Text & " has an updated image for this event.")
               Else
                   MsgBox("Unable to upload image to server for " & TextBox2.Text)
               End If

               connection.Close()


           Catch exa As Exception
               MsgBox(exa.ToString)
           End Try





任何帮助都会感激不尽,因为我不知道了。谢谢



any help would bre gratefully appreciated as I have no idea anymore. Thanks

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



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

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

就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

--'

其他一切都是评论。

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



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

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?