将图像数据库检索到datalist [英] Retrieving Image database to a datalist

查看:57
本文介绍了将图像数据库检索到datalist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是保存图像数据库的代码

  protected   void  BtnSave_Click( object  sender,EventArgs e)
{

SqlConnection con = new SqlConnection(connectionString);
if (FileUpload1.HasFile)
{
if (FileUpload1 .PostedFile.ContentType == image / bmp || FileUpload1.PostedFile.ContentType == image / jpg || FileUpload1.PostedFile.ContentType == image / jpeg || FileUpload1.PostedFile.ContentType == image / png
{
int filelenght = FileUpload1.PostedFile.ContentLength;
byte [] imagebytes = new byte < /跨度> [filelenght];
FileUpload1.PostedFile.InputStream.Read(imagebytes, 0 ,filelenght);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = 插入imagesave(图像)值(@img);
cmd.Connection = con;
cmd.Parameters.AddWithValue( @ img,imagebytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write( 图像保存到数据库);
}
}
}





 <   asp:DataList     ID   =  DataList1    runat   =  server    BorderStyle   =    宽度  =  634px    

< span class =code-attribute> < span class =code-attribute> BorderWidth = 1px RepeatColumns = 3 BackColor = 白色<\\ n / span> DataKeyField = ProductSNo

BorderColor = #CCCC C CellPadding = 4 ForeColor = 黑色

< span class =code-attribute> > ;

< 页脚 style backcolor = #CCCC99 forecolor = 黑色 / >
< HeaderStyle BackColor = #333333 字体-Bold = True ForeColor = 白色 < span class =code-attribute> / >
< itemtemplate >
< table cellspacing = 1 样式 = border-style :solid >
< tr >
< td align = center >
< asp:标签 ID = lblProductName runat = 服务器 文字 =' <%#DataBinder.Eval(Container.DataItem, ProductName)%>' Font-Bold = True 字体下划线 = True 字体大小 = X -Large > < br / > < span class =code-keyword>< hr / >

< < span class =code-leadattribute> / td >
< / tr >

< tr >
< td >


现在如何在datalist中重温图像:

< asp:Image < span class =code-attribute> ID = imgProduct ?? ???????< / td > < / tr > < / table > < / itemtemplate >

解决方案

离解决方案只有几行常规代码。如果您已经自己编写了INSERT代码,那么这应该不是一件好事。



从数据库中获取数据到Datatable对象并设置它作为DataList的数据源。



 使用(SqlDataAdapter da = < span class =code-keyword> new  SqlDataAdapter(  SELECT [image],ProductName FROM imagesave ,con)) //  如果它位于另一个表中,您可能需要使用JOIN来获取ProductName  
{
DataTable dt = new DataTable();
da.Fill(dt);
DataList1.DataSource = dt;
}





您可以使用简单的html img而不是ASP.Net Image。

< pre lang =HTML> < img src =' <% #DataBinder.Eval(Container.DataItem, image )%>' / >





注意:不建议使用关键字命名列,您可以考虑将列名改为类似ProductImage。



希望,它有帮助:)


This is the code of saving image database

protected void BtnSave_Click(object sender, EventArgs e)
        {
         
            SqlConnection con = new SqlConnection(connectionString);
            if (FileUpload1.HasFile)
            {
                if (FileUpload1.PostedFile.ContentType == "image/bmp" || FileUpload1.PostedFile.ContentType == "image/jpg" || FileUpload1.PostedFile.ContentType == "image/jpeg" || FileUpload1.PostedFile.ContentType == "image/png")
                {
                    int filelenght = FileUpload1.PostedFile.ContentLength;
                    byte[] imagebytes = new byte[filelenght];
                    FileUpload1.PostedFile.InputStream.Read(imagebytes, 0, filelenght);
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "Insert into imagesave(image) values(@img)";
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@img", imagebytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Response.Write("Image saved to database");
                }
            }
        }



  <asp:DataList ID="DataList1" runat="server" BorderStyle="None" Width="634px" 

                                            BorderWidth="1px" RepeatColumns="3" BackColor="White" DataKeyField="ProductSNo"

                                            BorderColor="#CCCCCC" CellPadding="4" ForeColor="Black" 

                         >
                                          
                                            <footerstyle backcolor="#CCCC99" forecolor="Black" />
                                            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                                            <itemtemplate>
                                                <table cellspacing="1" style="border-style: solid">
                                                    <tr>
                                                        <td align="center">
                                                <asp:Label ID="lblProductName" runat="server"  Text='<% #DataBinder.Eval(Container.DataItem, "ProductName")%>' Font-Bold="True" Font-Underline="True" Font-Size="X-Large"><br /><hr />

                                                        </td>
                                                   </tr>
             
                                                    <tr>
                                                        <td>


Now how  to reteieve image in datalist:

                                                        <asp:Image ID="imgProduct"   ?????????</td></tr></table></itemtemplate>

解决方案

You are just few lines of conventional code away from the solution. If you have written the code to INSERT by your own then it should not be a great task to do.

Get data from database in to a Datatable object and set it as datasource of the DataList.

using (SqlDataAdapter da = new SqlDataAdapter("SELECT [image],ProductName FROM imagesave", con)) //You may need to use JOIN to get ProductName if it resides in another table
{
    DataTable dt = new DataTable();
    da.Fill(dt);
    DataList1.DataSource = dt;
}



You can use simple html img instead of ASP.Net Image.

<img src='<% #DataBinder.Eval(Container.DataItem, "image")%>' />



Note: it is not recommended to use keywords for naming column, you can consider changing your column name to something like ProductImage.

Hope, it helps :)


这篇关于将图像数据库检索到datalist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆