2016-05-25 104 views
1
Image img = PictureBox1.Image; 
byte[] arr; 
ImageConverter converter = new ImageConverter(); 
arr = (byte[])converter.ConvertTo(img, typeof(byte[])); 

SqlCommand cmd = new SqlCommand("insert into tblWerknemers2 (Naam,Voornaam,Foto) values(@Naam,@[email protected])", con); 
cmd.Parameters.AddWithValue("@Naam", txtNaam.Text); 
cmd.Parameters.AddWithValue("@Voornaam", txtVoornaam.Text); 
cmd.Parameters.AddWithValue("@Foto", arr); 

這裏我插入圖像到數據庫中,但問題是我不知道如何檢索它。下面是我試圖做到這一點。如何使用ImageConverter將圖像從數據庫檢索到圖片框?

SqlDataReader read = command.ExecuteReader(); 

while (read.Read()) 
{ 
    txtNaam.Text = (read["Naam"].ToString()); 
    txtVoornaam.Text = (read["Voornaam"].ToString()); 
    arr = (byte[])read["Foto"];    
} 
read.Close(); 
+0

這裏是你如何做到這一點的一種方法:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=read%20image %20from%20database%20c%23 –

+0

但是,這是一個可能的重複:http://stackoverflow.com/questions/18998763/how-to-retrieve-binary-image-from-database-using-c-sharp-in- asp-net –

+0

還是無法弄清楚:/ – Viktor

回答

0
byte[] arr = (byte[])read["Foto"]; 
if (arr.Length > 0) 
{ 
    MemoryStream mem = new MemoryStream(arr); 
    PictureBox1.Image = Image.FromStream(mem); 
} 
相關問題