如何在Windows窗体应用程序中增加内存(Memory Stream)

时间:2017-12-07 01:05:44

标签: c# winforms memorystream

在下面实现这些代码后,我收到了“Out of Memory”错误。我真的很陌生,想问一下如何以及在哪里增加程序的内存空间。

using (SqlCommand cmd = new SqlCommand("select Background from Employee where EmployeeName = @Name", con))
            {
                cmd.Parameters.AddWithValue("@Name", txt_name.Text);
                con.Open();
                var dt = new DataTable();


                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    dt.Load(dr); // fill DataTable from reader
                }

                if (dt.Rows.Count > 0 && dt.Rows[0]["Background"] != DBNull.Value)
                {
                    using (MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["Background"]))
                    {
                        this.BackgroundImage = new Bitmap(ms);
                    }
                    // do another stuff
                }
                else
                {
                    // do another stuff
                }
            }con.Close();

你会非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

using (SqlCommand cmd = new SqlCommand("select Background from Employee where EmployeeName = @Name", con))
        {
            cmd.Parameters.AddWithValue("@Name", txt_name.Text);
            con.Open();
            var dt = new DataTable();


            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                dt.Load(dr); // fill DataTable from reader
            }

            if (dt.Rows.Count > 0 && dt.Rows[0]["Background"] != DBNull.Value)
            {
                using (MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["Background"]))
                {
                    this.BackgroundImage = new Bitmap(ms);
                }
                // do another stuff
            }
            else
            {
                // do another stuff
            }
        }con.Close();

归功于Kalyan