简体   繁体   中英

Image upload to database

So I managed to set-up a simple test page to upload and store image to my database however I am unsure of whether the storage is successful.

Whenever I store the image into my table under the column Image with the datatype Image , this is what is in the new row <Binary data> . Is this <Binary data> the image?

I was expecting it to display the image in '0' and '1' so I can compare the different items stored. But does having "" stored means that my image had been successfully stored?

My website's logic is coded in c#.

And also I had been trying to find sources with examples to how I may retrieve my image for display.

This is my current insert statement

SqlCommand com = new SqlCommand("insert into ImageTotable "
    + "(myphoto,name) values (@photo, @name)", con);

To retrieve the data will this work?

SqlCommand com2 = new SqlCommand("Select * from ImageTotable WHERE userid ='1'", con);

So if I use a datareader to store the selected items, what can I store my image to so that it will display, a label, image button, etc?

And how do I store the image into the variables? For example if I want to store text I would use:

pw = dr["password"].ToString();**  

Therefore for images what would it be like?

EDIT: Full button on click event to handle the image strage

    protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=*;Initial Catalog=*;Integrated Security=True");
    if (!FileUpload1.HasFile)
    {
        Label1.Visible = true;
        Label1.Text = "Please Select Image File";    //checking if file uploader has no file selected


    }
    else
    {
        int length = FileUpload1.PostedFile.ContentLength;
        byte[] pic = new byte[length];


        FileUpload1.PostedFile.InputStream.Read(pic, 0, length);

        try
        {


            con.Open();
            SqlCommand com = new SqlCommand("insert into ImageTotable "
              + "(myphoto,name) values (@photo, @name)", con);
            com.Parameters.AddWithValue("@photo", pic);
            com.Parameters.AddWithValue("@name", TextBox1.Text);
            com.ExecuteNonQuery();
            Label1.Visible = true;
            Label1.Text = "Image Uploaded Sucessfully";  //after Sucessfully uploaded image

        }
        finally
        {
             con.Close();
        }

    }
}

First, the Image type in Db maps to Byte[] in C#, so you should convert your image into Byte[] before inserting into your database. For retrieving your image from the database, you can use the code:

 MemoryStream stream = new MemoryStream(Byte[]);// you can read the image by dataadapter or datareader
 System.Drawing.Image img = System.Drawing.Image.FromStream(stream);

Here is a good link: http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html and hope it can help you.

This always helps me. 'fileupload' is the File Upload Control Name.

protected void Upload_btn(object sender, EventArgs e)
{
  if (fileupload.HasFile)
   {
     if (fileupload.PostedFile.FileName == "")
      {}
     else
      {
       try
        {
          int filesize = fileupload.PostedFile.ContentLength;
          if (filesize > 819200)
           {Label1.Text = "File Size Should be Less than 800Kb";
            }
           else
           {string serverFileName = Path.GetFileName(fileupload.PostedFile.FileName);
            byte[] documentBinary = new byte[filesize];
            fileupload.PostedFile.InputStream.Read(documentBinary, 0, filesize);

            string mainquery = "";
            mainquery = "insert into table(DocName,DocSize,Data,ContentType)       values(@DocName,@DocSize,@Data,@ContentType)";
            con.Open();
            SqlCommand files_insert_cmd = new SqlCommand(mainquery,con);
            files_insert_cmd.Parameters.AddWithValue("@DocName", serverFileName);
            files_insert_cmd.Parameters.AddWithValue("@DocSize",fileupload.PostedFile.ContentLength);
            files_insert_cmd.Parameters.AddWithValue("@Data", documentBinary);
         files_insert_cmd.Parameters.AddWithValue("@ContentType",fileupload.PostedFile.ContentType);
            files_insert_cmd.ExecuteNonQuery();

            con.Close();
         }
     }
  catch (Exception e1)
      {
         Label1.Text = e1.ToString();
         Label1.Visible = true;
      }

   }
 }
}

Sorry in advance, because i am about to ask a stupid question. How are you checking data in table? are you right clicking on table and selecting edit top 200 rows If you do like this, it will always show you as < binary Data > where as if you run sql query you can see actual characters in varbinary(max) column or Image column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM