c#sql数据库中的二进制图片

2024-11-18 20:18:21
推荐回答(1个)
回答1:

String sql = "select * from picture where [id]=2";
string str = "server=DA5A491B3F7544B;database=study;uid=sa;pwd=liyi123";
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd=new SqlCommand(sql,con);
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.HasRows)
{
sdr.Read();
byte[] bytes = (byte[]) sdr["pic"];
MemoryStream ms =new MemoryStream(bytes);
Image image = Image.FromStream(ms);
pictureBox1.Image = image;
}
sdr.Close();
con.Close();