我想在dbc中插入一个映像时删除进程内存#

yb3bgrhw  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(236)

我有以下代码将图像放入picturebox:

OpenFileDialog f = new OpenFileDialog();
            f.Filter = "JPG(*JPG)|*.jpg";
            if (f.ShowDialog() == DialogResult.OK)
            {

                pictureBox4.Image = Image.FromFile(f.FileName);
            }

下面的代码将图像插入数据库:

public void Team()//insert into db new teammate

    {
        try
        {
            MemoryStream ms = new MemoryStream();
            pictureBox4.Image.Save(ms, pictureBox4.Image.RawFormat);
            byte[] a = ms.GetBuffer();
            ms.Close();

            SqlConnection con = new SqlConnection(stringcon); //CONNECTION

            cmd.Parameters.Clear();
            cmd.Connection = con;
            cmd.CommandText = "INSERT INTO team(lastname,firstname,phonenumber,email,[password],[function],[role],registerdata,personaldescription,profilepicture) VALUES(@lastname,@firstname,@phonenumber,@email,@password,@function,@role,@registerdata,@personaldescription,@profilepicture)";

            cmd.Parameters.AddWithValue("@lastname", lastname_textbox.Text);
            cmd.Parameters.AddWithValue("@firstname", firstname_textbox.Text);
            cmd.Parameters.AddWithValue("@phonenumber", "+"+phone_textbox.Text);
            cmd.Parameters.AddWithValue("@email", email_textbox.Text);
            cmd.Parameters.AddWithValue("@password", repeatpassword_textbox.Text);
            cmd.Parameters.AddWithValue("@function", function_textbox.Text);
            cmd.Parameters.AddWithValue("@role", role_dropbox.selectedValue);
            cmd.Parameters.AddWithValue("@registerdata", DateTime.Now.ToString("yyyy-MM-dd HH: mm:ss"));
            cmd.Parameters.AddWithValue("@personaldescription", personaldescription_textbox.Text);
            cmd.Parameters.AddWithValue("@profilepicture", a);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
            ex.ToString();
            return;
        }
    }

现在,在db中插入图像后,我想清除进程内存,因为增加进程内存这和那不好,因为我想每次插入db时插入10个这样的图像。例如:我在初始化时有120mb的进程内存在我把一个图像插入picturebox后我有150MB,但是当我插入10个图像时我有120mb+30mb*10..但是我想我可以在插入db后清除这个内存,但是我不知道怎么做。

jfewjypa

jfewjypa1#

你应该去看电影 using 代码块
using语句简化了创建并最终清理对象所需的代码。using语句获取指定的资源,执行语句,最后调用对象的dispose方法来清理对象。
将代码移入 using 作为

using (SqlConnection con = new SqlConnection(connectionString))
  {
        MemoryStream ms = new MemoryStream();
        pictureBox4.Image.Save(ms, pictureBox4.Image.RawFormat);
        byte[] a = ms.GetBuffer();
        ms.Close();
        ...........
  }

幕后 using 语句被翻译成

try 
{
   .....
}
finally
{
   .....
}

相关问题