0

In our application, we are saving the Excel files uploaded by the users to the database with varBinary data type. But, we would also like to retrieve back the data and be able to parse it to get the data in it (columns, rows).

I already know how to read an Excel File into a DataTable to get the data from it(like the code below), but I don't know how to do it if the DataSource is of type varbinary.

// Getting data from actual Excel File
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);

var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();

adapter.Fill(ds);

foreach (DataTable dt in ds.Tables) {
    Response.Write(dt.TableName);
    int i= 0;
    foreach (DataRow dr in dt.Rows)
    {
        if (i < 2) { i++;  continue; }
        foreach (DataColumn dc in dt.Columns)
        {
            Response.Write(dc.ToString() + ": " + dr[dc.ToString()] + "<br />");
        }
        i++;
    }
}

Is it possible to do with SqlServer 2012 and .NET 4? If yes, how?

Any feedback would be greatly appreciated.

Regards, artsylar

1 Answer 1

0

The code you posted doesn't save an excel file into a table, it saves the contents of the excel file into a table. These are not the same.

In order to save the actual Excel file into a varbinary member of a table row, read the file from disk into a byte array, and then store the byte array into the varbinary. In the case of a Web application, use a file upload control to retrieve the file the user wishes to store to a temporary file storage area on the web server. Load the file to the database from there.

To reconstitute the file, retrieve the varbinary data into a byte array, and then write the byte array to a disk file. The file must be named appropriately, of course. In the case of a Web applciation, reconstitute the file in a local temporary location on the web server, and then send to the client's browser. They'll see a "save file" dialog, which will allow them to save the file locally on their machine.

6
  • The code I posted is actually getting the data from the Excel File only. It is not yet saving the data to the database. I want to the same with excel file converted to varbinary type.
    – artsylar
    Commented Jun 6, 2013 at 7:23
  • I was also able to create an Excel File from the varbinary data. But what I want to do now is to be able to parse the varbinary data without the need of saving it first to Excel File into the hard disk.
    – artsylar
    Commented Jun 6, 2013 at 7:25
  • So do you want to store the excel file, or do you want to store just the excel file's contents?
    – STLDev
    Commented Jun 6, 2013 at 7:25
  • I just want to get the contents of the varbinary data (originally excel file) and show it to the user.
    – artsylar
    Commented Jun 6, 2013 at 7:29
  • So if I understand this correctly, you've got the contents of an Excel file stored in a varbinary, and from that, you want to display this data in a grid or table on a web page, but you don't want to take the intermediate step of creating an excel file to accept the contents of the varbinary and then query it. Is that correct?
    – STLDev
    Commented Jun 6, 2013 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.