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: 44% [?]




January 29th, 2009 at 5:16 pm
i’m new to linq. can you please tell if there is any namespace needed for the use of
MyDataDataContext MyData = new MyDataDataContext();
January 29th, 2009 at 5:41 pm
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.
February 20th, 2009 at 6:08 pm
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();
September 1st, 2009 at 10:47 am
How do you diasplay the content of the uploaded file in ASP.net.
October 9th, 2009 at 11:01 am
But how to get back image from database and display in HTML Image tag?
October 30th, 2009 at 6:34 am
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 !