【问题标题】:how to retrieve binary image from database in vb.net and insert the image in grid view如何从 vb.net 中的数据库中检索二进制图像并将图像插入到网格视图中
【发布时间】:2011-09-03 17:47:58
【问题描述】:

如何使用 vb.net 从数据库中检索二进制图像并将图像插入到 GridView 中。

这是我的数据库

图像(id 为整数,img 为 varbinary(max))

【问题讨论】:

  • 你说的是什么类型的gridview? asp.net的gridview,winform的gridview?什么?

标签: vb.net image gridview binary


【解决方案1】:

虽然您明确了您所指的 gridview 的类型,但以下是在数据库中插入数据的方法:

Using c As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
   c.Open()
   Dim command = New SqlCommand("INSERT INTO yourtable(image) values (@image)", c)
   ' this is specific to the FileUploadControl but the idea is to get the
   'image in a byte array; however you do it, it doesn't matter
    Dim buffer(FileUpload1.PostedFile.ContentLength) As Byte
    FileUpload1.PostedFile.InputStream.Read(buffer, 0, buffer.Length)
    command.Parameters.AddWithValue("@image", buffer)
    command.ExecuteNonQuery()    
End Using

假设您正在谈论一个 ASP .NET 应用程序,您可以将数据绑定到 gridview,如下所示:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                     <!--Trick to encode the bytes as a BASE64 string-->
                    <img width="100px" height="100px" src='data:image/png;base64,<%#System.Convert.ToBase64String(Eval("image"))%>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="Data Source=Your_ConnectionString_GoesHere" 
            ProviderName="System.Data.SqlClient" 
            SelectCommand="SELECT [id], [image] FROM [your_table_name_goes_here]">
        </asp:SqlDataSource>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    相关资源
    最近更新 更多