2017-02-12 39 views
0

我想插入/刪除,使用asp.net更新sql數據庫中的圖像。我插入的代碼是:用asp.net插入/更新/從sql數據中刪除圖像

Stream strm = FileUpload1.PostedFile.InputStream; 
     BinaryReader br = new BinaryReader(strm); 
     byte[] img = br.ReadBytes(Convert.ToInt32(strm.Length)); 
     imgDetail.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(img, 0, img.Length); 
     imgDetail.Visible = true; 
     try 
     { 
      cmd = new SqlCommand("insert into Persons (id,Name,Surname,Address,Phone,Picture,Department) values (@id,@Name,@Surname,@Address,@Phone,@img,@Department)",con); 
      cmd.Parameters.AddWithValue("id", txtId.Text); 
      cmd.Parameters.AddWithValue("Name", txtName.Text); 
      cmd.Parameters.AddWithValue("Surname", txtSurname.Text); 
      cmd.Parameters.AddWithValue("Address", txtAddress.Text); 
      cmd.Parameters.AddWithValue("Phone", txtPhone.Text); 
      cmd.Parameters.AddWithValue("img", img); 
      cmd.Parameters.AddWithValue("Department", Department.Text); 

      con.Open(); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 

我覺得出事了與添加的圖像,也許我這樣做不對。現在當我在我的gridview的一些記錄點擊,在文本框記錄顯示的所有數據,但我不能在圖像箱中顯示圖像。我怎樣才能做到這一點? 這是在文本框displaing數據的一部分:

 foreach (GridViewRow row in GridView1.Rows) 
     { 
      if (row.RowIndex == GridView1.SelectedIndex) 
      { 
       GridViewRow row1 = GridView1.SelectedRow; 
       txtId.Text = row.Cells[0].Text; 
       txtName.Text = row.Cells[1].Text; 
       txtSurname.Text = row.Cells[2].Text; 
       txtAddress.Text = row.Cells[3].Text; 
       txtPhone.Text = row.Cells[4].Text; 
       Department.Text = row.Cells[5].Text; 
       Image.?????? 

回答

0

「圖像」控制是標準<img> HTML標籤周圍只是一個包裝。所以顯然它沒有任何二進制屬性,您可以設置爲一個字節數組。

但你用「數據」uri去。

這裏有一個主意,你:

Image.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])imagedata); 
相關問題