将图像从图片库保存到数据库的问题。 VB.Net 2008. Framework 3.5

我有一个包含显示图像名称列表的列表框的表单。 它绑定到数据库表。 当单击图像名称时,它将分别在图片框和文本框中显示图像和图像名称。 当在列表框中未选择图像时,可以通过openfiledialog在图像框中浏览新图像,将图像名称写入文本框并按下OK按钮来插入新记录。 当图像已被选中时,可以通过按下相同的OK按钮来更新记录。 数据保存到MSSQL Server 2005中。对应的表字段是Keycode int autono,logoname nvarchar(50),logo图像。 现在的问题是,当我用图像插入新数据时一切都很顺利,但每当我尝试用图像更新现有数据时,它都会引发异常 - “GDI +中发生了一般性错误”。 在下面一行 - 'pic.Image.Save(ms,pic.Image.RawFormat)'。 令人惊讶的是,当我更新现有数据而没有任何图像在图片框中时,不会产生异常。 我已经过检查它,似乎这个问题只是在一个点 - '更新图片框中的图像'。 我几乎完成了所有工作,但坚持了这一点。 请帮忙。 问候。

我的代码通过确定按钮插入/更新数据,并通过列表框doubleclick填充它:

Private ms As MemoryStream
Private arrImage() As Byte
Private conn As SqlConnection
Private cmd As SqlCommand

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Method to bind listbox.
    BindListBox(lst, "Select Keycode,LogoName from tbltest", "Logoname", "keycode")
        Tag = "Insert"
End Sub

Private Sub lst_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lst.DoubleClick
        Dim dr As SqlDataReader

        dr = CreateReader("Select LogoName,logo from tblTest where keycode=" & lst.SelectedValue)
        If dr.Read Then
            txtLogoName.Text = vbNullString & dr("Logoname")
            If Not IsDBNull(dr("Logo")) Then
                arrImage = CType(dr("Logo"), Byte())
                ms = New MemoryStream(arrImage)
                pic.Image = Image.FromStream(ms)
                ms.Close()
            Else
                pic.Image = Nothing
                pic.Invalidate()
            End If
            Tag = "Update"
        End If
        dr.Close()
        closeconnection()
        arrImage = Nothing
        ms = Nothing
End Sub

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim com As SqlCommand
        Dim strSql As String

        If Tag = "Insert" Then
            strSql = "Insert into tbltest (logoname,logo) values ('" & Trim(txtLogoName.Text) & "',@Logo)"
        Else
            strSql = "Update tbltest set logoname='" & Trim(txtLogoName.Text) & "',Logo=@Logo Where keycode=" & lst.SelectedValue
        End If

        com = CreateCommand(strSql)
        com.Parameters.Add(New SqlParameter("@Logo", SqlDbType.Image))
        If Not pic.Image Is Nothing Then
            ms = New MemoryStream()
            pic.Image.Save(ms, pic.Image.RawFormat)
            arrImage = ms.GetBuffer
            ms.Close()
            com.Parameters("@Logo").Value = arrImage
        Else
            com.Parameters("@Logo").Value = DBNull.Value
        End If

        If com.ExecuteNonQuery = 1 Then
            closeconnection()
            BindListBox(lst, "Select Keycode,LogoName from tbltest", "Logoname", "keycode")
            pic.Image = Nothing
            pic.Invalidate()
            txtLogoName.Clear()
            Tag = "Insert"
        End If


        arrImage = Nothing
        ms = Nothing
        strSql = Nothing
End Sub

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        With dlg
            .Filter = "All Files|*.*|Bitmap|*.bmp|GIF|*.gif|Icon|*.ico|JPEG|*.jpg|PNG|*.png"
            .FilterIndex = 5
        End With

        If dlg.ShowDialog() = DialogResult.OK Then pic.Image = Image.FromFile(dlg.FileName)
End Sub

Public Sub setconnection()
        Try
            conn = New SqlConnection("Data Source=MyServer;Initial Catalog=TestDB;User Id=sa;Password=;")
            conn.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
End Sub

Public Sub closeconnection()
        conn.Close()
End Sub

Public Function CreateCommand(ByVal query As String) As SqlCommand
        setconnection()
        Dim command As New SqlCommand(query, conn)
        Return command
End Function

Public Function CreateReader(ByVal query As String) As SqlDataReader
        Dim reader As SqlDataReader
        setconnection()
        cmd = CreateCommand(query)
        reader = cmd.ExecuteReader()
        Return reader
End Function

尽自己的一份忙,并将图像作为独立文件保存在数据库之外的共享网络驱动器上。 在数据库中只存储文件名。

这有两个好处:调试你的图像文件会容易得多,而且你的数据库将会更小,运行速度更快。


使用参数。 改变这个:

strSql = "Update tbltest set logoname='" & Trim(txtLogoName.Text) & "',Logo=@Logo Where keycode=" & lst.SelectedValue

strSql = "Update tbltest set logoname=@LogoName,Logo=@Logo Where keycode=@KeyCode"

然后,提供@LogoName和@KeyCode的参数值。

链接地址: http://www.djcxy.com/p/73421.html

上一篇: Problem in saving image to database from picturebox. VB.Net 2008. Framework 3.5

下一篇: Best way to pass a connection object among forms?