我想在将图像插入 db c# 时删除进程内存
I want to delete process memory when insert an image into db c#
我有以下代码可以将图像放入图片框中:
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 的进程内存,我有 150mb,但是当我插入 10 张图像时,我有 120mb+30mb*10..但我想我可以在插入数据库后清除此内存,但我不知道不知道怎么办。
你应该选择 using
代码块
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
中的代码移动为
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
{
.....
}
我有以下代码可以将图像放入图片框中:
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 的进程内存,我有 150mb,但是当我插入 10 张图像时,我有 120mb+30mb*10..但我想我可以在插入数据库后清除此内存,但我不知道不知道怎么办。
你应该选择 using
代码块
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
中的代码移动为
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
{
.....
}