How to retrive the images from Databaase?
this code is used to store the images in database usign stored procedures and this code also proved practically proved
1. go to sqlserver then
create table InsertPhoto(eid int identity(1,1),ename varchar(30),photo image)then excute it
after that write procedues in sql serever only
we should write like that
CREATE PROCEDURE sp_InsertPhoto(@ename varchar(30),@photoclip_IM image )ASINSERT INTO insertphoto ( ename,photo )VALUES ( @ename,@photoclip_IM )GO
then excute it sqlsever only
after that go to asp.net page
go to html controles
in that
take input(file) drop on the asp.net page
go to html source page make it runat="server" beside input file control and then
in that control only we should take input (submit) button
drop on the page
we should include this namespaces
using System.Data.OleDb;
using System.Drawing.Imaging ;
using System.IO;using System.Drawing;
in submit button_click event we write like this
Stream imagestreame = File1.PostedFile.InputStream;
int len = File1.PostedFile.ContentLength;
byte[] imagecontent = new byte[len];
imagestreame.Read(imagecontent, 0, len);
if (cn.State == ConnectionState.Closed)
{ cn.Open();
}
cmd=new OleDbCommand ("sp_InsertPhoto",cn);
cmd.CommandType = CommandType.StoredProcedure;
OleDbParameter pr = new OleDbParameter("@ename", OleDbType.VarChar);
pr.Value = File1.Value;
cmd.Parameters.Add(pr);
OleDbParameter primage = new OleDbParameter("@photoclip_IM", SqlDbType.Image );
primage .Value= imagecontent;
cmd.Parameters.Add(primage);
cmd.ExecuteNonQuery();
Response.Write("image stored");
}}
for retriving the same image from database we should write like this
you must include this name spaces
using System.Data.OleDb;
using System.Drawing.Imaging ;
using System.IO;using System.Drawing;
in button_click event
OleDbDataAdapter adp=new OleDbDataAdapter ();
DataSet ds=new DataSet ();
int i=0;
try{
cn .Open();cmd=new OleDbCommand ("select photo from insertphoto where eid=1",cn);
adp.SelectCommand =cmd ;
ds=new DataSet("st");
adp.Fill(ds);
byte [] bits= (byte [])(ds.Tables[0].Rows[i][0]);
//converting the data to byteMemoryStream memorybits= new MemoryStream(bits);
Bitmap bitm =new Bitmap(memorybits);
Response.BinaryWrite(bits);}catch(Exception ex){Response.Write(ex.Message);
}
finally
{
cn.Close();
}
}
}
}
No comments:
Post a Comment