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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
HakanPosted Jul 8, 2009, 7:19 AM
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
Suchit KhannaPosted May 26, 2009, 5:04 AM
Suchit KhannaPosted May 26, 2009, 5:01 AM
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);
}
}
Dorababu MekaPosted May 26, 2009, 3:10 AM
Suchit KhannaPosted May 26, 2009, 2:54 AM
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.