从picturebox将图像保存到数据库时出现问题。 VB.Net 2008.框架3.5

时间:2010-08-20 13:59:16

标签: vb.net visual-studio-2008

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

我的代码通过“确定”按钮插入/更新数据并按列表框填充 双击如下:

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

2 个答案:

答案 0 :(得分:2)

为数据库之外的共享网络驱动器上的单独文件保存自己的大忙。只存储数据库中的文件名。

这有两个好处:调试图像文件会更容易,数据库也会更小,运行速度更快。

答案 1 :(得分:0)

使用参数。改变这个:

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的参数值。