简体   繁体   English

“System.OutOfMemoryException: 'Out of memory.'”从 SQL 服务器读取图像时

[英]"System.OutOfMemoryException: 'Out of memory.'" when reading image from SQL Server

I have images assigned to every button in my VB.NET form, the images come from SQL Server.我的 VB.NET 表单中的每个按钮都分配了图像,图像来自 SQL 服务器。 The data type is varbinary(MAX) .数据类型是varbinary(MAX)

This is my code:这是我的代码:

Using con As New SqlConnection("con string")
    Dim sql As String = "SELECT * FROM Inventory WHERE ID=@ID"
    Using cmd As New SqlCommand(sql, con)
        cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 3
        con.Open()
        Using myreader As SqlDataReader = cmd.ExecuteReader()
            If myreader.Read() AndAlso Not DBNull.Value.Equals(myreader("Image")) Then
                Boton3.Text = myreader("Item")
                Boton3.Enabled = myreader("ONOFF")
                Dim ImgSql() As Byte = DirectCast(myreader("Image"), Byte())
                Using ms As New MemoryStream(ImgSql)
                    Boton3.BackgroundImage = Image.FromStream(ms)
                    con.Close()
                End Using
            Else
                Boton3.Text = myreader("Item")
                Boton3.BackgroundImage = Nothing
                Boton3.Enabled = myreader("ONOFF")
            End If
        End Using
    End Using
End Using

The platform is 64bit.该平台是64位的。 I'm thinking it might have to do with not disposing properly, but I'm not sure since I'm new to coding.我认为这可能与处理不当有关,但我不确定,因为我是编码新手。

EDIT SHOWING NEW CODE AND HOW I RETRIVE MORE THAN ONE RECORD:编辑显示新代码以及我如何检索多条记录:

Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        Dim dt As DataTable
        Try
            dt = GetInventoryDataByID(1)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try
        If dt.Rows.Count > 0 Then
            Boton1.Text = dt.Rows(0)("Articulo").ToString
            Boton1.Enabled = CBool(dt.Rows(0)("ONOFF"))
            If Not DBNull.Value.Equals(dt.Rows(0)("Imagen")) Then
                Dim ImgSql() As Byte = DirectCast(dt.Rows(0)("Imagen"), Byte())
                Using ms As New MemoryStream(ImgSql)
                    Boton1.BackgroundImage = Image.FromStream(ms)
                End Using
            Else
                Boton1.BackgroundImage = Nothing
            End If
        Else
            MessageBox.Show("No records returned")
        End If
        Dim dt2 As DataTable
        Try
            dt2 = GetInventoryDataByID(2)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try
        If dt2.Rows.Count > 0 Then
            Boton2.Text = dt2.Rows(0)("Articulo").ToString
            Boton2.Enabled = CBool(dt2.Rows(0)("ONOFF"))
            If Not DBNull.Value.Equals(dt2.Rows(0)("Imagen")) Then
                Dim ImgSql() As Byte = DirectCast(dt2.Rows(0)("Imagen"), Byte())
                Using ms As New MemoryStream(ImgSql)
                    Boton2.BackgroundImage = Image.FromStream(ms)
                End Using
            Else
                Boton2.BackgroundImage = Nothing
            End If
        Else
            MessageBox.Show("No records returned")
        End If
    End Sub
    Private Function GetInventoryDataByID(id As Integer) As DataTable
        Dim dt As New DataTable
        Dim sql As String = "SELECT Imagen, Articulo, ONOFF FROM Inventario WHERE ID=@ID"
        Using con As New SqlConnection("CON STRING"),
            cmd As New SqlCommand(sql, con)
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id
            con.Open()
            Using myreader As SqlDataReader = cmd.ExecuteReader()
                dt.Load(myreader)
            End Using
        End Using
        Return dt
    End Function
End Class

You don't want to hold a connection open while you update the user interface.您不想在更新用户界面时保持连接打开。 Separate you user interface code from your database code.将用户界面代码与数据库代码分开。

If you put a comma at the end of the first line of the outer Using block, both the command and the connection are included in same block.如果在外部 Using 块的第一行末尾放置一个逗号,则命令和连接都包含在同一个块中。 Saves a bit of indenting.节省了一点缩进。

You are passing an integer to the @ID parameter but you have set the SqlDbType as a VarChar .您将 integer 传递给@ID参数,但您已将SqlDbType设置为VarChar Looks like a problem.看起来有问题。 I changed the SqlDbType to Int .我将SqlDbType更改为Int

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt As DataTable
    Try
        dt = GetInventoryDataByID(3)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Exit Sub
    End Try
    If dt.Rows.Count > 0 Then
        Boton3.Text = dt.Rows(0)("Item").ToString
        Boton3.Enabled = CBool(dt.Rows(0)("ONOFF"))
        If Not DBNull.Value.Equals(dt.Rows(0)("Image")) Then
            Dim ImgSql() As Byte = DirectCast(dt.Rows(0)("Image"), Byte())
            Using ms As New MemoryStream(ImgSql)
                Boton3.BackgroundImage = Image.FromStream(ms)
            End Using
        Else
            Boton3.BackgroundImage = Nothing
        End If
    Else
        MessageBox.Show("No records returned")
    End If
End Sub

Private Function GetInventoryDataByID(id As Integer) As DataTable
    Dim dt As New DataTable
    Dim sql As String = "SELECT * FROM Inventory WHERE ID=@ID"
    Using con As New SqlConnection("con string"),
            cmd As New SqlCommand(sql, con)
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id
        con.Open()
        Using myreader As SqlDataReader = cmd.ExecuteReader()
            dt.Load(myreader)
        End Using
    End Using
    Return dt
End Function

I resolved this issue by simply not using buttons.我通过不使用按钮解决了这个问题。 Instead I used pictureboxes as buttons and that resolved the issue.相反,我使用图片框作为按钮,这解决了这个问题。 Im guesssing the problem is that buttons don't allow as much memory as pictureboxes.我猜问题是按钮不允许像图片框那样多的 memory 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 增加最大服务器内存以处理SQL Server上的System.OutOfMemoryException - Increasing max server memory to deal with System.OutOfMemoryException on SQL Server 打开Sql Server文件时出现System.OutOfMemoryException错误 - System.OutOfMemoryException error when I open the Sql Server file 尝试运行由SQL Server Management Studio生成的创建表脚本时出现System.OutOfMemoryException - System.OutOfMemoryException when trying to run a create table script generated by SQL Server Management studio SQL Server System.OutOfMemoryException +太多结果 - SQL Server System.OutOfMemoryException + too many outcomes 如何在SQL Server 2014中解决System.OutOfMemoryException - How to solve System.OutOfMemoryException in SQL Server 2014 查询大型SQL表时出现System.OutOfMemoryException - System.OutOfMemoryException when querying large SQL table 将SQL Server 2016升级到版本13.0.15700.28后,经常发生System.OutOfMemoryException - System.OutOfMemoryException occurs frequently after Upgrading SQL Server 2016 to version 13.0.15700.28 System.OutOfMemoryException:angularjs中抛出了类型'System.OutOfMemoryException'的异常 - System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown in angularjs 训练 ML.NET -- System.OutOfMemoryException - Training ML.NET -- System.OutOfMemoryException 使用localdb进行NUnit测试期间的System.OutOfMemoryException - System.OutOfMemoryException during NUnit tests with localdb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM