views:

331

answers:

3

Hi,

I have a table in which there are two columns : 1. import type, 2. Excel import template.

The second column - "Excel import template" should store the whole excel file.

How would I save excel file in databse...can I use binary datatype column, convert excel file to bytes and save the same ?

Thanks in advance !

A: 

Excel does support saving to XML. You could use this and store the sheet in an XML datacolumn, or a varchar(max)

edosoft
+1  A: 

Yes, you can use a binary file type. IMAGE VARBINARY(MAX) is likely to fit the purpose best.

Regarding how to "convert the Excel file to bytes" (it really is bytes from the beginning), we will need to know more about your programming environment in order to help. If you are using .NET, you should be able to do something like this:

var insert = new SqlCommand("INSERT INTO tbl (xls) VALUES (@xls)", conn);
insert.Parameters.AddWithValue("xls", File.ReadAllBytes("template.xls"));
insert.ExecuteNonQuery();
Jørn Schou-Rode
IMAGE is deprecated: don't use it
gbn
@gbn: Thanks for bringing that to my attention. I have updated my answer to reflect this, and I will keep this in mind when working with MSSQL forward :)
Jørn Schou-Rode
We use asp.net/c#/sqlserver 2005..can you a more elaborate code...Plz :)
@gowri: What part of my code sample is causing you trouble? It basically defines a SQL query (you will want to adapt this to your schema), adds a parameter containing the contents of the XLS file and executes the query. `File.ReadAllBytes()` is documented here: http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx
Jørn Schou-Rode
A: 
varbinary(max)

An Excel file is binary so you can't use characters like varchar(max)

And you can't use IMAGE because it's deprecated as MSDN says

gbn