如何在vb.net 2010中从数据库中检索图像 [英] How to retrieve image from database in vb.net 2010

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

问题描述

Hey Guys,



我已将图像保存到数据库中,如下所示。



Hey Guys,

I have saved my images into database like follow.

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim cmd As New SqlCommand("INSERT INTO Info VALUES(@name,@photo)", con)
        cmd.Parameters.AddWithValue("@name", TextBox1.Text)
        Dim ms As New MemoryStream()
        PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
        Dim data As Byte() = ms.GetBuffer()
        Dim p As New SqlParameter("@photo", SqlDbType.Image)
        p.Value = data
        cmd.Parameters.Add(p)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK)
       
    End Sub







现在,在btnSearch_Download的按钮点击事件中,我想按名称搜索图像和



通过向用户提供选项将保存到数据库中的图像下载到计算机硬盘上



保存文件对话框,用户可以在其中选择要保存的位置图片文件。



我使用的基本代码是






Now, On the button click event of "btnSearch_Download", I want to search image by name and

download the image saved into database to computer hard drive by giving option to user with

"save file" Dialogue box, where user can choose where he want to save a image file.

Basic code i have used is

Private Sub btnSearch_Download_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch_Download.Click
        cmd = New SqlCommand("select photo from Info where name='" & TextBox2.Text & "'", con)
        Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
        'Tell me What to do here?
        End If
    End Sub

推荐答案

参考:保存使用VB.NET从SQL Server数据库中检索图像 [ ^ ]


添加此项:Dim bitmap As New Bitmap(stream)



PictureBox1.Image = bitmap
Add this: Dim bitmap As New Bitmap(stream)

PictureBox1.Image = bitmap


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

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