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

2024年11月18日 20:18
有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();