I was having some trouble doing insert/update image or other binary data to access database using asp.net/c#. At last successfully accomplished it and learned some important tips that I want to share with you all.
Uploading Image to Access Database using Asp.net/C#:
At first You must have to have a Database table field of type 'OLE Object'. now come to the c# coding part:
First get the info you need to store. You can store only the image instead of its type if you know the type of the image being uploaded. But its wise to store the type also. After file posted from the fileupload control, get its size,type & convert the posted item into byte stream.
int imgLength = uploaderSignature.PostedFile.ContentLength;
byte[] data = new byte[imgLength];
string type = uploaderSignature.PostedFile.ContentType;
uploaderSignature.PostedFile.InputStream.Read(data,0,imgLength);
then make a connection to the access database. If you are having proble with making the connectionstring, visit www.connectionstrings.com . Here I used OleDB connection:
System.Data.OleDb.OleDbConnection ob=new System.Data.OleDb.OleDbConnection();
ob.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\ApecDB.mdb;";
Now create a command and add input parameter. Important thing to remember here, you can't insert the binary data using plain/string type sql query like
myCommand.ExecuteNonQuery("Update tblCompany SET "+column+"="+data+","+column+"_type="+type); this won't work at all. You must have to use parameter based command as like follows:
OleDbCommand myCommand = new OleDbCommand("Update tblCompany SET "+column+"=@img_stream,"+column+"_type=@img_type", ob);
// Mark the Command as a Text
myCommand.CommandType = CommandType.Text;
OleDbParameter img_stream = new OleDbParameter("@img_stream", OleDbType.Binary);
img_stream.Value = data;
myCommand.Parameters.Add(img_stream);
OleDbParameter img_Type = new OleDbParameter("@img_type", OleDbType.VarChar);
img_Type.Value = type;
myCommand.Parameters.Add(img_Type);
Then, its quite simple. open connection, execute query, close connection. If rows affected, then query successfull, return true, otherwise return false.
int affectedRows;
try
{
ob.Open();
affectedRows = myCommand.ExecuteNonQuery();
ob.Close();
}
catch (Exception exc)
{
return false;
}
if (affectedRows > 0)
{
return true;
}
else
{
return false;
}
Retrieving Image from Access Database using Asp.net/C#:
Retrieving image is quite easy compared to saving after using normal select query you have to define the content type & cast the data to byte & write it as Binary stream as follows:
Response.ContentType = dataRow["img_type"].ToString();
byte[] imageData = (byte[])dataRow[0]["img"];
Response.BinaryWrite(imageData);
You will find a working example on the following link that I found elsewhere:
http://rapidshare.com/files/289324545/InsertShowImageAccessDatabseAsp.netC_.rar.html
Also Remember:
* all image type isn't supported, so sometimes some are saved as "application/octetstream". they won't be retrieved in normal ways. You have to make a bitmap object from those kind og images & save them to database for retrieving them well. The follwing code snippet will show you how to make new image of another type:
Image originalImage = Image.FromStream(stream,true,true);
Bitmap targetImage = new Bitmap(originalImage, width, height);
with it, you can also resize an image to another size with custom width & height.
Subscribe to:
Post Comments (Atom)










0 comments:
Post a Comment