Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing images 1

Status
Not open for further replies.

MJV57

Programmer
Apr 18, 2009
87
CA
I have a form that I currently attach images to which become a field in the sql table. I understand this is not an effecient way to perform this and it is better to store the image outside the database and have a link to a storage directory. Can anyone help me with this concept and how my code would change I currently have the following code for an object:

private void attachBN_Click_1(object sender, EventArgs e)
{
try
{
OpenFileDialog open = new OpenFileDialog();

open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

if (open.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(open.FileName);
}

}

catch (Exception)
{

throw new ApplicationException("Failed loading image");

}

}

private void removepartBN_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
}
 
1st remove the try catch.
1. your swallowing the existing error that occurs.
2. your not handling the existing error.

as for your scenario, there isn't anything complicated about it. the "complex" part is making sure your database and file storage are in sync.
Code:
//assumes the connection is created and opened before this occurs
IDbTransaction tran;
try
{
   tran = connection.BeginTransation();
   using(var command = connection.CreateCommand())
   {
      command.CommandText = "sql statement"
      command.Parameters.Add(...);
      command.ExecuteNonQuery();
   }
   tran.Commit();
}
catch(AdoNetException e)
{
   tran.Rollback();
   if(File.Exists(file_name))
   {
      File.Delete(file_name);
   }
   throw;
}
catch(IOException e)
{
   tran.Rollback();
   if(File.Exists(file_name))
   {
      File.Delete(file_name);
   }
   throw;
}
finally
{
   tran.Dispose();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason, but I have to admit this is over my head. Would you be able to add some comments to help me understand the code. I appreciate it.
 
what part(s) do not make sense?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
To be quite honest all of the code is pretty new to me.
What is this BeginTransaction and trans.rollback do?
Thank you for your continued support.
 
BeginTransaction
Rollback
Commit

all deal with database transactions. If you are not familiar with these concepts I would research them online. For details about how a specific database handles transactions, find a forum specific to that database and ask the experts.

for example:
MS SQL and Oracle have a very robust transaction mechanism.
MySQL has one, but a last time I looked it was not stable.
MS Access does not have a concept of transaction at all.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top