5

I am working on a visual C# program for image processing.I am trying to add image to sql database using Visual C# (Windows forms) and ADO.NET.

I have converted the image to binary form with filestream method but the image bytes are not getting saved in database. At the database image column, it says < Binary data > and no data is getting saved!

I have tried many methods for inserting( with and without stored procedures..etc) but always getting the same thing at database.

private void button6_Click(object sender, EventArgs e)
{
   try
   {
      byte[] image = null;
      pictureBox2.ImageLocation = textBox1.Text;
      string filepath = textBox1.Text;
      FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
      BinaryReader br = new BinaryReader(fs);
      image = br.ReadBytes((int)fs.Length);
      string sql = " INSERT INTO ImageTable(Image) VALUES(@Imgg)";
      if (con.State != ConnectionState.Open)
         con.Open();
      SqlCommand cmd = new SqlCommand(sql, con);
      cmd.Parameters.Add(new SqlParameter("@Imgg", image));
      int x= cmd.ExecuteNonQuery();
      con.Close();
      MessageBox.Show(x.ToString() + "Image saved");
   }
}

I am not getting error in code. the image got converted and entry is done in database but says < Binary Data > at the sql database.

7
  • 3
    Why don't you just save the image to disk? Commented Nov 6, 2013 at 16:37
  • It's not clear in what way this isn't working. If there's binary data stored in the database, isn't that successful? Or do you mean it's storing the literal string "<Binary data>"? Show the code you're using to store the data, and indicate where you notice there's a problem. Commented Nov 6, 2013 at 16:37
  • Show code. If you show us both how you convert the image to bytes and how you're trying to insert it, there's a bigger chance we can find the problem. Commented Nov 6, 2013 at 16:38
  • Post your code, we could guess all day as to why it's not working (but we won't). Also, do you need to save the image to the database? Or can you simply write the location of the image instead? Commented Nov 6, 2013 at 16:40
  • yes..only the literal string is there. Its empty when i have tried to convert it back to image using memorystream. Commented Nov 6, 2013 at 16:41

4 Answers 4

5

The selected file stream should be converted to byte array.

        FileStream FS = new FileStream(filepath, FileMode.Open, FileAccess.Read); //create a file stream object associate to user selected file 
        byte[] img = new byte[FS.Length]; //create a byte array with size of user select file stream length
        FS.Read(img, 0, Convert.ToInt32(FS.Length));//read user selected file stream in to byte array

Now this works fine. Database still shows < Binary data > but it could be converted back to image using memorystream method.

Thanks to all...

Sign up to request clarification or add additional context in comments.

Comments

2

Try with something like this:

byte[] fileBytes=System.IO.File.ReadAllBytes("path to file");
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("insert  into table(blob,filename) values (@blob,@name)");
command.Parameters.AddWithValue("blob", fileBytes);
command.Parameters.AddWithValue("name", "filename");
command.ExecuteNonQuery();

Comments

1

Assuming that you are using VARBINARY to store your image (which you should be), you may need to make your SqlParameter more strongly typed:

So instead of

cmd.Parameters.Add(new SqlParameter("@Imgg", image));

You would use:

cmd.Parameters.Add("@Imgg", SqlDbType.VarBinary).Value = (SqlBinary)image;

You could also use System.IO.File.ReadAllBytes to shorten your code turning a file path into a byte array.

Comments

0

Execute this stored procedure:

create procedure prcInsert
(
   @txtEmpNo varchar(6),
   @photo image
)
as
begin
   insert into Emp values(@txtEmpNo, @photo)
end

Table Emp:

create table Emp
(
   [txtEmpNo] [varchar](6) NOT NULL,
   imPhoto image
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.