如何从SQL/VB.NET上传/检索图像 [英] How to upload/retrieve images from SQL/VB.NET

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

问题描述

我想使用vb.net从SQL数据库上载和检索图像

form1_case 1.必须使用form1中的按钮选择图像
form1_case 2.选择图像后,我将使用保存按钮将其上传到SQL数据库
form1_case3. SQL数据库表中有两列. (1)image_id(int),图片(image).

form2_case 1.从第二个form2中,我将搜索上载到SQL数据库的图像的ID.
form2_care2.该图像将被放置在picturebox1.image中.

笔记.请提供一种更简单的方法.

我尝试过的事情:

I want to upload and retrieve images from SQL database using vb.net

form1_case 1. Images must be selected by using a Button in form1
form1_case 2. After selection of images Im gonna use a save button to upload it to SQL database
form1_case3. There are two columns in SQL database table. (1) image_id (int), picture(image).

form2_case 1. From a Second form2 Im gonna search the id of the images that is uploaded to SQL database.
form2_care2. That image will be placed in a picturebox1.image.

Note. Please provide an easier method.

What I have tried:

Imports System.IO
Imports System.Data.SqlClient

Public Class fac_reg

    Dim connection As SqlConnection
    Dim command As SqlCommand
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        connection = New SqlConnection("HIDDEN")
        Dim reader As SqlDataReader
        Try
            connection.Open()
            Dim defText As String = "Not Modified".ToString
            Dim getDate As Date = Date.Now.ToString(" yyyy-MM-dd ")
            Dim facDOB As Date = DateTimePicker1.Value.ToString(" yyyy-MM-dd ")
            Dim query As String
            query = "INSERT into faculty(id,dept,sub,pre,name,dob,sex,addr,city,state,pin,country,nat,mob,email,uname,pword,sec,ans,image,reg,mod)values('" & TextBox1.Text & "','" & ComboBox5.Text & "','" & ComboBox4.Text & "','" & ComboBox1.Text & "','" & TextBox2.Text & "','" & facDOB & "','" & ComboBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox14.Text & "','" & TextBox5.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox10.Text & "','" & TextBox12.Text & "','" & ComboBox3.Text & "','" & TextBox13.Text & "','@image','" & getDate & "','" & defText & "')"
            Dim ms As New MemoryStream
            PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
            command.Parameters.Add("@image", SqlDbType.Image).Value = ms.ToArray
            command = New SqlCommand(query, connection)
            reader = command.ExecuteReader
            MsgBox("Faculty registration completed")
            connection.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            TextBox1.Clear() : TextBox2.Clear() : TextBox3.Clear() : TextBox4.Clear() : TextBox5.Clear() : TextBox6.Clear() : TextBox7.Clear() : TextBox8.Clear() : TextBox9.Clear() : TextBox10.Clear() : TextBox11.Clear() : TextBox12.Clear() : TextBox13.Clear() : TextBox14.Clear()
            ComboBox1.Text = String.Empty
            ComboBox2.Text = String.Empty
            ComboBox3.Text = String.Empty
            ComboBox4.Text = String.Empty
            ComboBox5.Text = String.Empty
            PictureBox1.Image = Nothing
        End Try
        connection.Dispose()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim openbrowser As New OpenFileDialog
        openbrowser.Filter = "Choose Image(*.jpg;*.png)|*.jpg;*.png"
        If openbrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(openbrowser.FileName)
        End If
    End Sub
End Class

推荐答案

尝试此博客-
[
try this blog-
[^]


string filePath = Server.MapPath("APP_DATA/TestDoc.docx");

string filename = Path.GetFileName(filePath);

 

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

BinaryReader br = new BinaryReader(fs);

Byte[] bytes = br.ReadBytes((Int32)fs.Length);

br.Close();

fs.Close();


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

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