Hay Friend's
Can u plese send me the way how to save image in sql server and display that images in datagrid or other control also like Image control or Image control Button?? Plese send the coding in C#.
Thank's
Amit
try this link...it display image in data grid...
http://www.odetocode.com/Articles/172.aspx
and for further reference you can refer
http://forums.asp.net/p/1122135/1754824.aspx#1754824
|||Hi amit, thanks for ur link but I m not fully Satisfy.. so kindly Send me more solutions..Thank'sAmit|||Hi Amit, Thanks for ur Link, but I m not fully Satisfy. So kindly send me more solutions ThanksAmit|||
Hi,
You can use stream object to insert the image to your database. Here's the sample for you to refer.
1.Store image in database:
Int64 intImageSize;string strImageType;Stream ImageStream;// Gets the Size of the ImageintImageSize = PersonImage.PostedFile.ContentLength;// Gets the Image TypestrImageType = PersonImage.PostedFile.ContentType;// Reads the ImageImageStream = PersonImage.PostedFile.InputStream;byte[] ImageContent =new byte[intImageSize + 1];int intStatus;intStatus = ImageStream.Read(ImageContent, 0, intImageSize);// Create Instance of Connection and Command ObjectSqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"));SqlCommand myCommand =new SqlCommand("sp_person_isp", myConnection);// Mark the Command as a SPROCmyCommand.CommandType = CommandType.StoredProcedure;// Add Parameters to SPROCSqlParameter prmPersonImage =new SqlParameter("@.PersonImage", SqlDbType.Image);prmPersonImage.Value = ImageContent;myCommand.Parameters.Add(prmPersonImage);SqlParameter prmPersonImageType =new SqlParameter("@.PersonImageType", SqlDbType.VarChar, 255);prmPersonImageType.Value = strImageType;myCommand.Parameters.Add(prmPersonImageType);try {myConnection.Open();myCommand.ExecuteNonQuery();myConnection.Close();Response.Write("New person successfully added!");}catch (SqlException SQLexc) {Response.Write("Insert Failed. Error Details are: " + SQLexc.ToString());}2. Retrieving Images from SqlServer and displaying in a DataGrid
|||<asp:TemplateColumn HeaderText="Image"> <ItemTemplate> <asp:Image Width="150" Height="125" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "PersonID"))%>' Runat=server /> </ItemTemplate></asp:TemplateColumn>//And the method FormatURL (A Server side Function) is as follows.public string FormatURL(object strArgument){return ("readrealimage.aspx?id=" + strArgument);}//And the method FormatURL (A Server side Function) is as follows.public string FormatURL(object strArgument){return ("readrealimage.aspx?id=" + strArgument);}///Database partpublic void Page_Load(object sender, EventArgs e){string strImageID = Request.QueryString("id");// Create Instance of Connection and Command ObjectSqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"));SqlCommand myCommand =new SqlCommand("Select PersonImageType, PersonImage from das_person_real_images Where PersonID=" + strImageID, myConnection);// I have used the select statement. But it would be much much better, if you //could write a small stored procedure which contains the above sql statement.// Mark the Command as a SPROC (in case, if you wrote the stored procedure//myCommand.CommandType = CommandType.StoredProceduretry {myConnection.Open();SqlDataReader myDataReader;myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);while ((myDataReader.Read())) {Response.ContentType = myDataReader.Item("PersonImageType");Response.BinaryWrite(myDataReader.Item("PersonImage"));}myConnection.Close();}catch (SqlException SQLexc) {}}Hope that helps. Thanks.
Hi, this is an interesting post. I am curious about if there is something similar you can do to display the SQL image field in a DataGrid in a Windows Forms application too?
Thanks in advance.
Jim
No comments:
Post a Comment