Click here to Skip to main content
15,888,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello members,
i need some help that,how to save a image file in a database from front end...my front end is VC#...the following is my code but image file is not saved in database...

C#
Program.Connection2Server();
            string updt = "";

            FileStream fs = new FileStream(curFileName,FileMode.OpenOrCreate,FileAccess.Read);
            //MessageBox.Show(fs.Length.ToString());

            byte[] rawdata=new byte[fs.Length];
            fs.Read(rawdata,0,System.Convert.ToInt32(fs.Length));
            fs.Close();
            updt = "update Faculty_details set Photo='"+rawdata+"'";
            Program.con.Close();



plz guyz help me out....
Posted
Comments
Mycroft Holmes 6-Mar-12 3:42am    
All you are doing is reading the file into a byte array, you now need to write it to the database. Try searching for an answer, this has been done many times and there are plenty of resources out there.
[no name] 6-Mar-12 3:47am    
you mean to say,i have to explicitly write the array elements...???

i suggest not to save a file in a DB.. instead of it use a file path & stored it in DB.

FileStream st = new FileStream(@"C:\filename.jpg", FileMode.Open);
byte[] buffer = new byte[st.Length];
st.Read(buffer, 0, (int)st.Length);
st.Close();



SqlConnection conn = new SqlConnection("...");
SqlCommand cmd = new SqlCommand("UPDATE SomeTable SET image=@image WHERE ID = 1", conn);
cmd.Parameters.AddWithValue("@image", buffer);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
 
Share this answer
 
Comments
[no name] 6-Mar-12 4:12am    
i think this code would be working...i'll try it....
[no name] 6-Mar-12 4:20am    
int i = cmd1.ExecuteNonQuery();

this line is givin an exception--> must declare sacalar variable @image

why?
Sushil Mate 6-Mar-12 4:27am    
use this method cmd.Parameters.Add instead of cmd.Parameters.AddWithValue

and another check table coloumn name you are not making spelling mistake there?
Sushil Mate 6-Mar-12 4:28am    
put your code here... let me see
[no name] 6-Mar-12 4:46am    
FileStream st = new FileStream(curFileName, FileMode.Open);
byte[] buffer = new byte[st.Length];
st.Read(buffer, 0, (int)st.Length);
st.Close();



Program.Connection2Server();
SqlCommand cmd1 = new SqlCommand("UPDATE Faculty_details SET photo=@image WHERE Faculty_code = 1", Program.con);
SqlParameter param;

param=cmd1.Parameters.Add(new SqlParameter("@image", buffer));

cmd1.ExecuteNonQuery();



Program.con.Close();

its working but sometimes it takes null values....
yes i solved the problem:


C#
FileStream st = new FileStream(curFileName, FileMode.Open);
            byte[] buffer = new byte[st.Length];
            st.Read(buffer, 0, (int)st.Length);
            st.Close();



            Program.Connection2Server();
            SqlCommand cmd1 = new SqlCommand("UPDATE Faculty_details SET photo=@image WHERE Faculty_code ='"+textBox1.Text+"'", Program.con);
            SqlParameter param;

            param=cmd1.Parameters.Add(new SqlParameter("@image", buffer));
            
            int i=cmd1.ExecuteNonQuery();
            Program.con.Close();


and its working....
 
Share this answer
 
Comments
OriginalGriff 6-Mar-12 6:34am    
Yep! That's a parametrized query!
Now, do the same for your faculty code, before your database gets destroyed by user text entry...
And if you think I'm joking, google "Bobby Tables"...
[no name] 6-Mar-12 8:34am    
thanx sir for suggesting the hint.....
Use a parametrized query.
If you build your SQL command by concatenation (as you are doing) then there are two problems:
1) You put your whole database at risk from an accidental or deliberate SQL injection attack that could damage or destroy it very, very easily.
2) It won't work, as the raw image bytes will be processed by the SQL server as command data and the whole command rejected as rubbish.
 
Share this answer
 
Comments
[no name] 6-Mar-12 4:03am    
couldn you tell me with a little example...coz its going over my head....
check the below link it answers your concern.

http://www.dotnet-guide.com/save-image-in-sql.html
 
Share this answer
 
Comments
[no name] 6-Mar-12 4:05am    
i didnt get what i wanted...

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