Main menu

Uploading Binary files or Images using LINQ to SQL

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.
 

Aneef Fashir (39 Posts)

Software Architect @ Assette, Sri Lanka


  • Shyam

    i’m new to linq. can you please tell if there is any namespace needed for the use of

    MyDataDataContext MyData = new MyDataDataContext();

  • Shyam

    i’m new to linq. can you please tell if there is any namespace needed for the use of

    MyDataDataContext MyData = new MyDataDataContext();

  • http://www.aneef.net/ Aneef Fashir

    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.

  • http://www.aneef.net Aneef Fashir

    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.

  • 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();

  • 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();

  • Pingback: Linq to Sql: Upload binary file « R in Random

  • Nuwan

    How do you diasplay the content of the uploaded file in ASP.net.

  • Nuwan

    How do you diasplay the content of the uploaded file in ASP.net.

  • http://www.doyouknow.in/ DoYouKnow.IN

    But how to get back image from database and display in HTML Image tag?

  • http://www.doyouknow.in DoYouKnow.IN

    But how to get back image from database and display in HTML Image tag?

  • 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 !

  • 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 !

  • http://fullmeals.net vijai

    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.

  • Miru

    I Just need to Upload Images using Linq to sql in silverlight..Can any One help me!

  • miru

    How can I read the content of image in silverlight?

  • Soulchyld

    Hi, I need to store hundreds of powerpoint files in a database, is it possibe to do this using sql syntax, that way I can duplicate it using excell and save myself some time! If it is would you be able to hlp me with the syntax?

Tags

Archives