Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Table ilke this

StudentId int,
Name char(20),
Student_img image
I want to store details with image in database and retrive it using windows forms

thx in advance....
Posted

 
Share this answer
 
Comments
Abhinav S 28-Jan-12 4:27am    
My 5.
Espen Harlinn 28-Jan-12 8:34am    
5'ed!
Store is easy: use a parametrized query, and hand the byte content of the image to the database via an INSERT query:
C#
MemoryStream ms = new MemoryStream();
myImage.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.ToArray();
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (StudentId, Name, Student_img) VALUES (@ID, @NM, @IM)", con))
        {
        com.Parameters.AddWithValue("@ID", Id);
        com.Parameters.AddWithValue("@NM", Name);
        com.Parameters.AddWithValue("@IM", bytes);
        com.ExecuteNonQuery();
        }
    }
Retrieve is no harder:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT StudentId, Name, Student_img FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["StudentId"];
                string desc = (string) reader["Name"];
                byte[] bytes = (byte[]) reader["Student_img"];  
                MemoryStream ms = new MemoryStream(bytes);
                myImage = Image.FromStream(ms);
                }
            }
        }
    }
 
Share this answer
 
Comments
Ravi Sargam 28-Jan-12 4:34am    
thx Griff.....
thatraja 28-Jan-12 5:25am    
5!
Espen Harlinn 28-Jan-12 8:34am    
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900