0

我想将图像上传到数据库,我在互联网上找到了很多代码,但没有一个对我有用。我正在使用格式(png、gif、jpeg 和 bmp)的按钮浏览图像浏览图像,然后我想使用按钮保存将这些类型的图像上传到数据库。
为了取回它们,我使用另一个按钮加载。你能指导我如何做到这一点。
这是我的编码告诉我我哪里错了。

Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Image

Public Class Employee

Dim myImage As Image
Dim imgMemoryStream As IO.MemoryStream = New IO.MemoryStream()
Dim imgByteArray As Byte() = Nothing
Dim con As SqlConnection = New SqlConnection("Data Source=CILENTEYEZ-PC\CILENTEYEZ;Initial Catalog=Keeper;Integrated Security=True")
Dim cmd As SqlCommand
Dim myDA As SqlDataAdapter
Dim myDataSet As DataSet
Dim dr As SqlDataReader

' From Here This is the Code for Insertion.

Private Sub BrowsePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowsePic.Click

    Dim fd As OpenFileDialog = New OpenFileDialog()

    fd.Title = "Select your Image."
    fd.InitialDirectory = "C:\"
    fd.Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg|PNGs|*.png"
    fd.RestoreDirectory = False

    If fd.ShowDialog() = DialogResult.OK Then
        PictureBox.ImageLocation = fd.FileName
    ElseIf fd.FileName.Contains("jpeg") Or fd.FileName.Contains("jpg") Then

        myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
        imgByteArray = imgMemoryStream.GetBuffer()

    ElseIf fd.FileName.Contains("png") Then

        myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Png)
        imgByteArray = imgMemoryStream.GetBuffer()

    ElseIf fd.FileName.Contains("gif") Then

        myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Gif)
        imgByteArray = imgMemoryStream.GetBuffer()

    ElseIf fd.FileName.Contains("bmp") Then

        myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Bmp)
        imgByteArray = imgMemoryStream.GetBuffer()

    End If

End Sub

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

cmd = New SqlCommand("Insert Into Employee Values('" & SR_CodeTextBox.Text.Trim() & "','" & NameTextBox.Text.Trim() & "','" & CNICTextBox.Text.Trim() & "','" & Date_of_BirthDateTimePicker.Text & "','" & AgeTextBox.Text.Trim() & "','" & AddressTextBox.Text.Trim() & "',@ imgByteArray ,'" & Mobile_NumberTextBox.Text.Trim() & "')", con)
        If con.State = ConnectionState.Closed Then con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
        MessageBox.Show("New Employee Record is Added.", "Message", MessageBoxButtons.OK)

End Sub

' From Here This is the Code for Eiditing.

Private Sub Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go.Click

    cmd = New SqlCommand("Select * from Employee where SR_Code = '" & SearchBox.Text.Trim() & "'", con)

    If SearchBox.Text = "" Then
        MsgBox("Please enter SR Code first.")

    Else

        Try

            If con.State = ConnectionState.Closed Then con.Open()
            dr = cmd.ExecuteReader()

            If dr.HasRows = True Then

                dr.Read()

                Edit_SR_CodeTextBox.Text = dr.Item("SR_Code")
                Edit_NameTextBox.Text = dr.Item("Name")
                Edit_CNICTextBox.Text = dr.Item("CNIC")
                Edit_Date_of_BirthDateTimePicker.Text = dr.Item("Date_of_Birth")
                Edit_AgeTextBox.Text = dr.Item("Age")
                Edit_AddressTextBox.Text = dr.Item("Address")
                imgByteArray = dr.Item("Picture")
                Edit_Mobile_NumberTextBox.Text = dr.Item("Mobile_Number")

                imgMemoryStream = New IO.MemoryStream(imgByteArray)
                myImage = Drawing.Image.FromStream(imgMemoryStream)
                PictureBox.Image = myImage

                MsgBox("Record Retrieved.")
                con.Close()

            ElseIf dr.Read = False Then
                MsgBox("Please enter a valid SR Code to load data.")
                con.Close()
            End If

        Catch ex As Exception
            MessageBox.Show(ex, "Message")

        End Try

    End If

End Sub
4

1 回答 1

0

对于图像浏览,您可以使用此代码。

    Private Sub BrowsePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowsePic.Click

    Dim ImageDialogue As OpenFileDialog = New OpenFileDialog()

    ImageDialogue.Title = "Select your Image."
    ImageDialogue.InitialDirectory = "C:\"
    ImageDialogue.Filter = "Image Files|*.gif;*.jpg;*.png;*.bmp;"
    ImageDialogue.RestoreDirectory = False

    If ImageDialogue.ShowDialog() = DialogResult.OK Then
        PictureBox.Image = Image.FromFile(ImageDialogue.FileName)
    End If

End Sub  

对于图像上传(到数据库),您可以在 VB 2010 中使用此代码。

cmd = New SqlCommand("Insert Into Proposer Values(& @Picture )", con)
If con.State = ConnectionState.Closed Then con.Open()

            Dim ms As New MemoryStream()
            PictureBox.Image.Save(ms, PictureBox.Image.RawFormat)
            Dim data As Byte() = ms.GetBuffer()
            Dim p As New SqlParameter("@Picture", SqlDbType.Image)
            p.Value = data
            cmd.Parameters.Add(p)
            cmd.ExecuteNonQuery()

            con.Close()

对于图像检索(从数据库),我在 VB 2010 中使用了此代码。

Dim data As Byte() = DirectCast(dr("Picture"), Byte())  
Dim ms As New MemoryStream(data)  
Edt_PictureBox.Image = Image.FromStream(ms)  

注意:这是在 VB-2010 中使用的,未在 VB-2008 上尝试过。正如您所看到的,代码与 C# 有点不同,所以不要指望它可以在 C# 上运行。
我可以进行更改(意味着转换为 C#)然后它会起作用。

于 2014-04-02T22:36:26.753 回答