1
如何使用vb.net從數據庫檢索二進制圖像並將圖像插入到GridView中。如何在vb.net中從數據庫檢索二進制圖像並在網格視圖中插入圖像
這是我的DB
圖像(ID爲整數,IMG爲varbinary(MAX))
如何使用vb.net從數據庫檢索二進制圖像並將圖像插入到GridView中。如何在vb.net中從數據庫檢索二進制圖像並在網格視圖中插入圖像
這是我的DB
圖像(ID爲整數,IMG爲varbinary(MAX))
當你明確你是指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>
你在說什麼類型的gridview? asp.net的gridview,Winform的gridview?什麼? – Icarus