2011-09-03 92 views

回答

0

當你明確你是指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> 
相關問題