Store/Retrieve image from DataBase
Store Image:
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
byte[] ImgBytes = new byte[flurl.PostedFile.InputStream.Length];
flurl.PostedFile.InputStream.Read(ImgBytes, 0, ImgBytes.Length);
string qry = "Insert into Table1(ImageData) values(@ImageData)";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@ImageData", ImgBytes);
cmd.ExecuteNonQuery();
con.Close();
Retrieve Image:
private string dumpImageToFile ( ref Byte[] img )
{
string strTempPath = System.IO.Path.GetTempPath ();
// Create the file name
string strFileName = strTempPath + "\\" + ".img";
// Copy raw data to a memory stream
System.IO.FileStream fs = new FileStream( strFileName, FileMode.OpenOrCreate, FileAccess.Write );
int offset = 0;
fs.Write( img, offset, img.Length - offset );
// Close stream
fs.Close();
fs = null;
return strFileName;
}
catch ( System.IO.IOException ioEx)
{
throw ioEx;
}
catch ( Exception ex)
{
throw ex;
}
}
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
byte[] ImgBytes = new byte[flurl.PostedFile.InputStream.Length];
flurl.PostedFile.InputStream.Read(ImgBytes, 0, ImgBytes.Length);
string qry = "Insert into Table1(ImageData) values(@ImageData)";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@ImageData", ImgBytes);
cmd.ExecuteNonQuery();
con.Close();
Retrieve Image:
private string dumpImageToFile ( ref Byte[] img )
{
string strTempPath = System.IO.Path.GetTempPath ();
// Create the file name
string strFileName = strTempPath + "\\" + ".img";
// Copy raw data to a memory stream
System.IO.FileStream fs = new FileStream( strFileName, FileMode.OpenOrCreate, FileAccess.Write );
int offset = 0;
fs.Write( img, offset, img.Length - offset );
// Close stream
fs.Close();
fs = null;
return strFileName;
}
catch ( System.IO.IOException ioEx)
{
throw ioEx;
}
catch ( Exception ex)
{
throw ex;
}
}
Comments
Post a Comment