Everything I have found shows how to add a row to a database for new images...
I am trying to update an existing row with a longblob field to store a logo and cannot seem to grasp what to do
Any help would be greatly appreciated
Everything I have found shows how to add a row to a database for new images...
I am trying to update an existing row with a longblob field to store a logo and cannot seem to grasp what to do
Any help would be greatly appreciated
Take a look at this thread:
http://www.daniweb.com/forums/thread247583.html
MeSampath provided this code:
MySqlConnection mcon = null;
MySqlCommand cmd = null;
FileStream fsObj = null;
BinaryReader binRdr = null;
try
{
//converting image to bytes
fsObj = File.OpenRead(pictureBox1.ImageLocation);
byte[] imgContent = new byte[fsObj.Length];
binRdr = new BinaryReader(fsObj);
imgContent = binRdr.ReadBytes((int)fsObj.Length);
mcon = new MySqlConnection("server=localhost;user=root;pwd=root;database=test;");
mcon.Open();
//inserting into MySQL db
cmd = new MySqlCommand("insert into users (userid,username,userphoto) values (@userid, @username, @userphoto)", mcon);
cmd.Parameters.Add(new MySqlParameter("@userid", (object)textBox1.Text));
cmd.Parameters.Add(new MySqlParameter("@username", (object)textBox2.Text));
cmd.Parameters.Add(new MySqlParameter("@userphoto", (object)imgContent));
MessageBox.Show(cmd.ExecuteNonQuery().ToString() + " rows affected");
}
catch (MySqlException mex)
{
MessageBox.Show(mex.Message);
}
finally
{
if (binRdr != null)binRdr.Close();
binRdr = null;
if (fsObj != null)fsObj.Close();
fsObj = null;
if (cmd != null)cmd.Dispose();
cmd = null;
if (mcon != null)
{
if (mcon.State == ConnectionState.Open)
mcon.Close();
}
}
That code inserts a new image. You can change the INSERT query to an UPDATE query to modify an existing image:
MySqlConnection mcon = null;
MySqlCommand cmd = null;
FileStream fsObj = null;
BinaryReader binRdr = null;
try
{
//converting image to bytes
fsObj = File.OpenRead(pictureBox1.ImageLocation);
byte[] imgContent = new byte[fsObj.Length];
binRdr = new BinaryReader(fsObj);
imgContent = binRdr.ReadBytes((int)fsObj.Length);
mcon = new MySqlConnection("server=localhost;user=root;pwd=root;database=test;");
mcon.Open();
//inserting into MySQL db
//cmd = new MySqlCommand("insert into users (userid,username,userphoto) values (@userid, @username, @userphoto)", mcon);
cmd = new MySqlCommand("update users set userphoto = @userphoto where userid = @userid", mcon);
cmd.Parameters.Add(new MySqlParameter("@userid", (object)textBox1.Text));
//cmd.Parameters.Add(new MySqlParameter("@username", (object)textBox2.Text));
cmd.Parameters.Add(new MySqlParameter("@userphoto", (object)imgContent));
MessageBox.Show(cmd.ExecuteNonQuery().ToString() + " rows affected");
}
catch (MySqlException mex)
{
MessageBox.Show(mex.Message);
}
finally
{
if (binRdr != null)binRdr.Close();
binRdr = null;
if (fsObj != null)fsObj.Close();
fsObj = null;
if (cmd != null)cmd.Dispose();
cmd = null;
if (mcon != null)
{
if (mcon.State == ConnectionState.Open)
mcon.Close();
}
}
As always Scott you are right on target...
I was having diffcultiies in wording the update statement and inserting the binary for the picture
Thank you again. Rep up to you.
using Windows 7 and calling:
OpenFileDialog LogoLoad = new OpenFileDialog();
LogoLoad.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
LogoLoad.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*";
LogoLoad.Title = "Load Dealer Logo";
LogoLoad.FilterIndex = 1;
LogoLoad.RestoreDirectory = true;
if (LogoLoad.ShowDialog() == DialogResult.OK)
{
filenname = Path.GetFileName(LogoLoad.FileName);
path = Path.GetDirectoryName(LogoLoad.FileName);
extension = Path.GetExtension(LogoLoad.FileName);
Problem is picturebox1.ImageLocation comes up as Null even though picture is displayed and path+filename does not give correct path IE..
It says c:\Users\username\Pictures\logo.gif
when it is actually c:\Users\username\My Pictures\Pictures\logo.gif
any clues here... cant wrap my mind why it would be giving wrong path yet displaying proper picture
Thank you for the help mate
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.