How to convert and save the image in database
Posted Date Unknown manu s 3 Comments

I need to save an image to database but how can i done it?

See All Responses Below...
Message Rating
Posted on: 10/07/2013
Convert the image to byte and store to db , better to save the image to a folder and use the image path , when you need it as a byte just convert when processing
Posted on: 10/07/2013
You can also save it as a Base 64 encoded string. You might need to use memo(unlimited text) type as a filed in SQL Database


Share this page across other social networking sites

Posted on: 12/03/2014
Use following code to convert image to bytes 
if(txt_fileupload.Text!="")  // use an open file dialog to set required image path to txt_fileupload
                filename = txt_fileupload.Text.Substring(Convert.ToInt32(txt_fileupload.Text.LastIndexOf("\")) + 1, txt_fileupload.Text.Length - (Convert.ToInt32(txt_fileupload.Text.LastIndexOf("\")) + 1));
                filetype = txt_fileupload.Text.Substring(Convert.ToInt32(txt_fileupload.Text.LastIndexOf(".")) + 1, txt_fileupload.Text.Length - (Convert.ToInt32(txt_fileupload.Text.LastIndexOf(".")) + 1));
                byte[] FileBytes = null;
                FileStream FS = new FileStream(txt_fileupload.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                BinaryReader BR = new BinaryReader(FS);
                long allbytes = new FileInfo(txt_fileupload.Text).Length;
                FileBytes = BR.ReadBytes((Int32)allbytes);
                string st = "insert into Files1(File_name,File_content,File_type) values ('" + filename + "','" + FileBytes + "','" + filetype + "')";
                con.insert1(st); // function to insert data to db
                MessageBox.Show("Images saved successfully");
                txt_fileupload.Text = "";
Please Login to Post Your Comments
Name (Max. 100 characters)
Please post your comments here
Select Language
Attach File(Max. Size: 2 MB)
A few simple rules when posting your Comments,
  1. Please post only answers relevant to the topic of discussion.
  2. Please dont misuse this site or do not be abusive, offensive, inappropriate,harass anyone on the boards or post ads or spam. Doing so will delete your inappropriate messages and will block or delete your account on this site.