把图片从数据库读出来的实现

byte[] aaa= null;

   SqlConnection conn= new SqlConnection();
   conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings[0];

   string queryStr = "select top 1 photo from table2(nolock)";
   SqlCommand commd = new SqlCommand(queryStr,conn);

   try
   {
    conn.Open();
    object bbb = commd.ExecuteScalar();
    aaa = bbb as byte[];
    MemoryStream ms = new MemoryStream(aaa);
    Image image=Image.FromStream(ms,true);
    pictureBox1.Image=image;
    ms.Flush();
    ms.Close();
   }
   catch(Exception ex)
   {
    throw ex;
   }
   finally
   {
    conn.Close();
   }

你可能感兴趣的:(数据库)