RSS

Uploading Binary files or Images using LINQ to SQL

Fri, Jan 16, 2009

LINQ, SQL, visual studio 2008

Today I received an email from a friend, asking how to save a binary file in to the database using LINQ to SQL.  This was the second time a person had asked me the same question, So I thought of having a blog entry about it.

Assume that we need to upload a file  and save its name, and the file in the database table. so lets see how we do it.

This is my Table

CREATE TABLE [dbo].[Files2](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [FileName] [varchar](50) NOT NULL,
    [FileSource] [image] NOT NULL,
 )
GO
And The Following Stored Procedure is used to Insert the Value:
 
CREATE PROCEDURE [dbo].[ADDFILE]
(
    @FILENAME VARCHAR(50),
    @FILESOURCE image
)
AS
SET NOCOUNT ON

INSERT INTO Files([FileName],[FileSource])
VALUES(@FILENAME,@FILESOURCE)

And the following code is used to upload and save the file using web form with a text box for file name, and a file upload control to upload the file. on the click event of a button this code is called.

protected void Button1_Click(object sender, EventArgs e)
{

     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!";

        }
}
 
 

The Main thing to remember here is that we need to pass the FileUploader.FileBytes  to the constructor of System.Data.Linq.Binary.

To Learn more about LINQ to SQL Please visit Here.

 EDIT: Richie just showed another way to achieve this in his blog here.
 

Popularity: 50% [?]

This post was written by:

Aneef Fashir - who has written 34 posts on Aneef.Net.

Software Engineer, Sri Lanka.

Contact the author

  • Works like a charm. You can set the "FILESOURCE" field as "Delay Loaded" if you dont want to load the file every time the entity is fetched.
  • kingofswing
    Also new to linq2sql, does MyData direct to call stored_procedure ADDFILE in the database? or do we need to drag the ADDFILE sp into the O/R designer?
    another question is how to read from database, and display it on page in asp.net? anyway, thanks a lot !
  • But how to get back image from database and display in HTML Image tag?
  • Nuwan
    How do you diasplay the content of the uploaded file in ASP.net.
  • Richie
    Great work Aneef,

    I have a table in the DB called Documents. Instead of creating the stored proc, I just let LINQ do everything for me... Your "byte[] filebyte = FileUploader.FileBytes;" is just what I needed.

    Document d = new Document
    {
    guid=Guid.NewGuid(),
    file_ext=ext.Replace(".","").Substring(0,3),
    file_nm=file_name,
    file_bin=file_binary,
    is_active=true,
    upload_page_type=rblLocation.SelectedValue,
    upload_dt=DateTime.Now,
    upload_by=Common.CurrentUserLogin
    };

    db.ControlDocuments.InsertOnSubmit(d);
    db.SubmitChanges();
  • Shyam
    i'm new to linq. can you please tell if there is any namespace needed for the use of

    MyDataDataContext MyData = new MyDataDataContext();
  • Hi Shyam,

    LINQ Namespace is enough for this. you have to add a LINQ to SQL Class and Name it as MyData, this will create your MyDataDataContext Class for your use.
blog comments powered by Disqus
Follow me