gpt4 book ai didi

c# - 我想在将图像插入 db c# 时删除进程内存

转载 作者:行者123 更新时间:2023-11-29 18:01:00 26 4
gpt4 key购买 nike

我有下面的代码将图像放入图片框中:

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;
}
}

现在,在数据库中插入图像后,想要清除进程内存,因为增加进程内存这个和那个不好,因为我每次插入数据库时​​都想插入 10 个这样的图像。例如:在将图像插入图片框后,我在初始化时有120mb的进程内存,我有150mb,但是当我插入10张图像时,我有120mb+30mb*10..但我认为我可以在插入数据库后清除该内存,但我不这样做不知道怎么办。

最佳答案

您应该使用使用代码块

The using statement simplifies the code that you have to write to create and then finally clean up the object. The using statement obtains the resource specified, executes the statements and finally calls the Dispose method of the object to clean up the object.

将代码移动到 using as

  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
{
.....
}

关于c# - 我想在将图像插入 db c# 时删除进程内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48348044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com