简体   繁体   中英

ASP.NET image(BLOB) retrieve from database(mysql)

I need to retrieve image from database and show in gridview here the code for ObjectDataSource associated with gridview for better understanding giving this code...thanks in advance

public static List<Users> GetAllUser()
{
   List<Users> UsersList = new List<Users>();

   string connection_string = "datasource=localhost;port=3306;username=root;password=root;";
   using (MySqlConnection connection = new MySqlConnection(connection_string))
   {
       MySqlCommand command = new MySqlCommand("select * from smart_shop.users order by id desc", connection);
       connection.Open();
       MySqlDataReader reader = command.ExecuteReader();
       while (reader.Read())
       {
          Users userObject = new Users();

          userObject.userName = reader["name"].ToString();
          userObject.image = reader["image"].ToString();   //Here variable 'image' is string type but in database BLOB            

          UsersList.Add(userObject);
       }

   }

   return UsersList;
}

and the gridview code is

<div class="col-md-6 col-sm-12 col-xs-12">
    <div class="panel panel-default">
        <h3>&nbsp;&nbsp;User Information</h3>
        <div class="panel-body">

            <asp:GridView ID="GridView2" CssClass="dataGridTable" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource2" AllowPaging="True" Width="100%" CellPadding="4" HeaderStyle-Height="30" ForeColor="#333333" GridLines="None" OnRowCreated="GridView2_RowCreated" PageSize="20">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="userName" HeaderText="User Name" SortExpression="userName" />
                    <asp:TemplateField HeaderText="Image" SortExpression="image">
                        <EditItemTemplate>
                            <asp:Image ID="Image1" runat="server" Height="100px" Width="100px" />
                        </EditItemTemplate>
                    </asp:TemplateField>

                </Columns>

                <EditRowStyle BackColor="#7C6F57" />
                <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" Height="30px" VerticalAlign="Middle" />
                <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#E3EAEB" VerticalAlign="Middle" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F8FAFA" />
                <SortedAscendingHeaderStyle BackColor="#246B61" />
                <SortedDescendingCellStyle BackColor="#D4DFE1" />
                <SortedDescendingHeaderStyle BackColor="#15524A" />
            </asp:GridView>


            <br />
            <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllUser" TypeName="OnlineSmartShop.SettingsDataAccessLayer"></asp:ObjectDataSource>

        </div>
    </div>
</div>

and finally

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
     {
            byte[] bytes = (byte[])(e.Row.DataItem as DataRowView)["image"];
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
            (e.Row.FindControl("Image1") as Image).ImageUrl = "data:image/png;base64," + base64String;
    }
}

reader["image"] contains byte[]

 while (reader.Read())
   {
      Users userObject = new Users();

      userObject.userName = reader["name"].ToString();
      userObject.image = Convert.ToBase64String((byte[])reader["image"]);              

      UsersList.Add(userObject);
   }

and change itemdatabound event. row item type is Users..

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Users rowUser = e.Row.DataItem as Users;

            (e.Row.FindControl("Image1") as Image).ImageUrl = "data:image/png;base64," + rowUser.image;
        }
    }

PS: This method can cause performance problems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM