Saving Image In To SQL
How to retrieve and save the image from SQL database can any tell me the easiest method
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Suchit KhannaRank #1,100 · 1.7K reputationPosted May 25, 2009, 10:54 PM EDT
Considering VS 2008-->
The easiest way would be to get your Image and convert it into bytestream and store the name of the image and byte stream(Image) to Sql Server where your sql table contains column of type image to support storage of the byte stream and another simple column of type Varchar to store the corresponding name of the image....
While retrieving it back you could query the table using simple SELECT statement and get the data in form of byte array and use BitmapImage to get the image back and supply it to Image control Source property.
Dorababu MekaRank #457 · 5.5K reputationPosted May 25, 2009, 11:10 PM EDT
Suchit KhannaRank #1,100 · 1.7K reputationPosted May 26, 2009, 1:01 AM EDT
private byte[] ConvertImageToByte(string fileName)
{
FileStream fstream = new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader reader = new BinaryReader(fstream);
byte[] image = reader.ReadBytes((int)fstream.Length);
return image;
}
private string StoreImageToDB(byte[] data, string name)
{
MySqlCommand command = getCommand("usp_InsertImage");
MySqlParameter param1 = new MySqlParameter();
param1.ParameterName = "pImage";
param1.Value = data;
command.Parameters.Add(param1);
MySqlParameter param2 = new MySqlParameter();
param2.ParameterName = "pName";
param2.Value = name.Substring(name.LastIndexOf("\\") + 1);
command.Parameters.Add(param2);
try
{
command.ExecuteNonQuery();
return "Saved Successfully...";
}
catch (Exception ex)
{
return ex.Message;
}
}
private Object GetImage()
{
MySqlCommand command = getCommand("usp_GetImage");
MySqlParameter param = new MySqlParameter();
param.ParameterName = "pID";
param.Value = txtID.Text;
command.Parameters.Add(param);
try
{
MySqlDataReader reader = command.ExecuteReader();
return reader;
}
catch (Exception ex)
{
return ex.Message;
}
}
private MySqlCommand getCommand(string spName)
{
MySqlConnection connection = new MySqlConnection();
connection.ConnectionString = "Server=localhost;Database=test;Uid=root;Pwd=password";
if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
MySqlCommand command = new MySqlCommand();
command.Connection = connection;
command.CommandText = spName;
command.CommandType = System.Data.CommandType.StoredProcedure;
return command;
}
private void Save(object sender, RoutedEventArgs e)
{
MessageBox.Show(StoreImageToDB(ConvertImageToByte(txtImageFile.Text), txtImageFile.Text));
}
private void Get(object sender, RoutedEventArgs e)
{
try
{
MySqlDataReader reader = (MySqlDataReader)GetImage();
if (reader.Read())
{
MemoryStream mStream = new MemoryStream((byte[])reader["Image"]);
BitmapImage img = new BitmapImage();
img.BeginInit();
img.StreamSource = mStream;
img.EndInit();
imgSource.Source = img;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Suchit KhannaRank #1,100 · 1.7K reputationPosted May 26, 2009, 1:04 AM EDT
Hakan2 reputationPosted Jul 8, 2009, 3:19 AM EDT
Error 1 The type or namespace name 'RoutedEventArgs' could not be found (are you missing a using directive or an assembly reference?)
and
Error 3 'System.Drawing.Image' does not contain a definition for 'Source' and no extension method 'Source' accepting a first argument of type 'System.Drawing.Image' could be found (are you missing a using directive or an assembly reference?)
I have used the above code, but changed datareader as I'm using SQL compact edition.
/Hakan