[英]ASP.NET image(BLOB) retrieve from database(mysql)
我需要從數據庫中檢索圖像,並在此處的gridview中顯示與gridview相關的ObjectDataSource的代碼,以便更好地理解給出此代碼的原因……在此先感謝
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;
}
和GridView代碼是
<div class="col-md-6 col-sm-12 col-xs-12">
<div class="panel panel-default">
<h3> 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>
最后
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”]包含byte []
while (reader.Read())
{
Users userObject = new Users();
userObject.userName = reader["name"].ToString();
userObject.image = Convert.ToBase64String((byte[])reader["image"]);
UsersList.Add(userObject);
}
並更改itemdatabound事件。 行項目類型為“用戶”。
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: 此方法可能會導致性能問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.