Select image from db ms sql c#

  • 378 Views
  • Last Post 04 January 2019
Stylus STYLUS posted this 01 January 2019

Need to select image from db on my code result is System.Byte[]

I need to display picture

Some help?  Thank you  

string memorandum = "";  

            using (SqlConnection openCon = new SqlConnection(con))  
            {  
                SqlDataReader reader;  
                SqlCommand cmd = new SqlCommand("SELECT memorandum from dbo.podaci_o_korisniku");  

                cmd.Connection = openCon;  
                openCon.Open();  
                reader = cmd.ExecuteReader();  

                while (reader.Read())  
                {  
                    byte[] ap = (byte[])podaci_o_korisnikuDataGridView.SelectedRows[0].Cells["memorandum"].Value;  
                    memorandum = reader[0].ToString();  

                }  
                openCon.Close();  
            }  
Order By: Standard | Newest | Votes
jack posted this 03 January 2019

I think you should change your code as the following

byte[] ap = (byte[])reader["memorandum"];

Next, You can also convert from the byte array into image type

Stylus STYLUS posted this 04 January 2019

I try this and result is

System.Byte[]

No have image

 string memorandum = "";

            using (SqlConnection openCon = new SqlConnection(con))
            {
                    SqlDataReader reader;
                    SqlCommand cmd = new SqlCommand("SELECT memorandum from dbo.podaci_o_korisniku");
                    cmd.Connection = openCon;
                    openCon.Open();

                byte[] img = (byte[])cmd.ExecuteScalar();
                MemoryStream str = new MemoryStream();
                str.Write(img, 0, img.Length);
                Bitmap bit = new Bitmap(str);
                reader = cmd.ExecuteReader();

                while (reader.Read())
                    {

                    byte[] ap = (byte[])reader["memorandum"];
                    memorandum = reader[0].ToString();
                    }
                     openCon.Close();
            }
Close