0

I have this below code to put an image into picturebox:

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

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

and this code below to insert the Image into database:

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

Now, after insert image in db in want to clear process memory because increase process memory this and that's not good because I want to insert 10 image like this every time I insert into db. ex: I have process memory 120mb at initialize after I insert an image into picturebox I have 150 mb but when I insert 10 image I have 120mb+30mb*10..but I think I can clear this memory after insert into db but I don't know how.

  • 2
    You have a number of sub optimal things going on there. a) Do not use GetBuffer - it will store up to 2ce as many bytes as are needed. Use `ToArray()` b) never store passwords as plain text c) never store dates as string d) never swallow exceptions e) use `Add(string, dbType)` rather than `AddWithValue` f) Dispose of your command and connection to prevent leaks. In general you might want to just store a path to the archived filename to prevent bloating the DB and access the image faster. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Jan 19 '18 at 19:22
  • thanks for these advices – Razvan Barbu Jan 20 '18 at 10:08

1 Answers1

0

You should go for using code block

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.

Move your code in 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();
        ...........
  }

Behind the scene using statement gets translated into

try 
{
   .....
}
finally
{
   .....
}
Ehsan Ullah Nazir
  • 1,827
  • 1
  • 14
  • 20