none
how to save or upload image files to database by using linq RRS feed

  • Question

  • I am trying images files to database by using linq but it does not work. i dont know why. 

    I retrieve image binaries by using GetBytesFromFile that I define. Save this to System.Data.Linq.Binary fileBinary 

    Andthen, try to upload this binary data to database. It operated without a error but when I check database, I could not find any

    update. if you know about this problem, plz let me know. Thank you in advance.

    ------------------------------------------------------------------------------------------------------------------------------------------------

     ImageUploadWindow imageUploadController = new ImageUploadWindow();
                UserDatabaseClassesDataContext dataContext = new UserDatabaseClassesDataContext();
                Images_table newRawImage = new Images_table();
                imageUploadController.ShowDialog();
                if (imageUploadController.DialogResult.HasValue && imageUploadController.DialogResult.Value)
                {


    byte[] filebyte = GetBytesFromFile(imageUploadController.fileNameTextBox.Text);

                    try
                    {
                        System.Data.Linq.Binary fileBinary = new System.Data.Linq.Binary(filebyte);
                        newRawImage.imageFiles = fileBinary;
                        newRawImage.imageNames = imageUploadController.imageNameTextBox.Text;




                        dataContext.Images_tables.InsertOnSubmit(newRawImage);
                        dataContext.SubmitChanges();
                        MessageBox.Show("Row Updated Successfully.");
                    }
                    catch (Exception err)
                    {


                        MessageBox.Show(err.ToString());
                    }
                }
                else
                {

                }
            }






            public byte[] GetBytesFromFile(string fullFilePath)
            {
                // this method is limited to 2^32 byte files (4.2 GB)


                FileStream fs = File.OpenRead(fullFilePath);
                try
                {
                    byte[] bytes = new byte[fs.Length];
                    fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                    fs.Close();
                    return bytes;
                }
                finally
                {
                    fs.Close();
                }

                
    • Moved by Sheldon _Xiao Friday, February 10, 2012 6:04 AM (From:Windows Presentation Foundation (WPF))
    Wednesday, February 8, 2012 4:32 PM

Answers

All replies

  • What you are describing could be because of several things.

    First is the database that you are using a SQL Server database that is attached to the server or is it a local database file.  In this case make sure that you are not copying a blank copy of the file on each execution.  I have spent hours on occasion with this problem.  Your app will work but on the next execution it copies a blank copy and you start over.

    If that is not the case can you check the GetChangeSet function of your datacontext.  If all values are zero then perhaps your definition in your DBML file are incorrect.

    Hope this helps

    Lloyd Sheen


    Lloyd Sheen

    Wednesday, February 8, 2012 4:57 PM
  • Hiya

    How are you storing the image? Do you use  'image' or 'varbinary(MAX)' column type? These both work fine IMO.
    Implementation looks ok, if you don't get any errors, you'll have to give us more to go on.
    Don't you need GetChangeSet or InsertOnSubmit or something?

    Regards,
    Pedro


    If you find my post helpful, please remember to "Mark As Answer" and/or "Vote as Helpful"

    Wednesday, February 8, 2012 4:58 PM
    Moderator
  • As you reported, I am using a local database file. But How can I prevent my application from copying a blank copy. Can you explain about detail. Otherwise, if you provide code sniffet that I need to change, I will appriciate.
    Thursday, February 9, 2012 2:55 PM
  • Hello

    Actually, I am using image column type in my database. And I don't know what information you want. I already used InserOnSubmit. what other do you want? 

    Thank you,

    Alex

    Thursday, February 9, 2012 3:01 PM
  • Is the database in your project?  If so check the properties of the database.  You will most likely see that it is set to be copied each time your execute.  Since you would have a copy (the copy you want) in your "bin" folder just change the property to not copy.  

    If you need to start the database over you can change the setting for one execution which will refresh the database.

    Hope this helps

    LS


    Lloyd Sheen

    Thursday, February 9, 2012 4:15 PM
  • check following code

    if (FileUploader.HasFile && FileUploader.PostedFile.ContentLength > 0)
    {
    string filename = txtFileName.Text;
    //Read the file in to a byte Array.
    byte[] filebyte = FileUploader.FileBytes;

    System.Data.Linq.Binary fileBinary = new System.Data.Linq.Binary(filebyte);
    MyDataDataContext MyData = new MyDataDataContext();

    MyData.ADDFILE(filename, fileBinary);
    lblStatus.Text = "File Uploaded Successfully!";

    }

    Wednesday, February 15, 2012 8:36 AM
  • Hi celinaasouza,

    Please try:

    private void buttonStoreImageToDb_Click(object sender, EventArgs e)
    {
    	// Open the DataContext
    	Database1 db = new Database1("Data Source=Database1.sdf");
    	try
    	{
    		// Convert System.Drawing.Image to a byte[]
    		byte[] file_byte = ImageToByteArray(pictureBox1.Image);
    		// Create a System.Data.Linq.Binary - this is what an "image" column is mapped to
    		System.Data.Linq.Binary file_binary = new System.Data.Linq.Binary(file_byte);
    		Images img = new Images
    		{
    			Image = file_binary,
    			ImageName = "Erik testing "
    		};
    		db.Images.InsertOnSubmit(img);
    	}
    	finally
    	{
    		// Save
    		db.SubmitChanges();
    	}
    }
    

    You can refer this blog:http://beyondrelational.com/blogs/community/archive/2010/08/17/how-to-save-and-retrieve-images-using-linq-to-sql-with-sql-compact.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 15, 2012 9:20 AM
    Moderator