.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Image Store in Database in BLOB Field

Posted By:varun bansal       Posted Date: July 27, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Image Store in Database in BLOB Field.

There is two process of image store in web application..

1. physicaly store
2. Store in Database (Blob)
I give the code of store image in database (Blob)..
//when the image has a valid path
if(imgname!="")
{
FileStream fs;
fs=new FileStream(@imgname,FileMode.Open,FileAccess.Read);
//a array of byte to read the image
byte[] blobimg = new byte[fs.Length];
fs.Read(blobimg,0,System.Convert.ToInt32(fs.Length));
fs.Close();
// open database and excute below query...
// emp is a table name where three column is id (nvarchar), name(nvarchar) and photo(img)
query="insert into emp(id,name,photo) values(" + txtid.Text + "," + "'" + txtname.Text + "'," + " :BlobParameter )";
OracleParameter blobParameter = new OracleParameter();
blobParameter.OracleType = OracleType.Blob;
blobParameter.ParameterName = "BlobParameter";
blobParameter.Value = blobimg;
cmnd=new OracleCommand(query,conn);
cmnd.Parameters.Add(blobParameter);
cmnd.ExecuteNonQuery();
MessageBox.Show("Succesfully done!");
}


Read img from database
There is use following contros:

1. combobox:cmbname
2. label:lblname
3. buttons:btnshow
4. picturebox:img
global declare these variable:

OracleConnection con;
OracleDataAdapter empadp;
DataSet ds;
string constr;
//bind name of img in combobox at page load event
constr="User Id=sc;Password=t;Data Source=st;";con=new OracleConnection(connstr);
con.Open();
empadp=new OracleDataAdapter();
empadp.SelectCommand=new OracleCommand("SELECT * FROM emp",conn);
ds=new DataSet("dset");
empadp.Fill(ds);
DataTable dt;
dt=ds.Tables[0];
cmbname.Items.Clear();
foreach(DataRow dr in dt.Rows)
{
cmbname.Items.Add(dr[1].ToString());
cmbname.SelectedIndex=0;
}

// show img when select name of img from combobox,
DataTable dt = ds.Tables[0];
//if there is an already an image in picturebox, then delete it
if(img.Image != null)
{
img.Image.Dispose();
}
//using filestream object write the column as bytes and store it as an image
FileStream FS = new FileStream("image.jpg", FileMode.Create);
foreach(DataRow dr in dt.Rows)
{
if(dr[1].ToString() == cmbname.SelectedItem.ToString()) //check name of the image
{
byte[] blob = (byte[])dr[2];
FS.Write(blob,0,blob.Length);
FS.Close();
FS = null;
img.Image = Image.FromFile("image.jpg");
img.SizeMode = PictureBoxSizeMode.StretchImage;
img.Refresh();
}
}
Regards
Varun bansal
www.varunbansal007.blogspot.com
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend