How to map Excel to SQL Server (dynamic SQL)?

Posted on 2008-11-10
Medium Priority
Last Modified: 2012-05-05
I have a page that allows you to upload an Excel file. The server will then select the records from Excel and load them into a grid like this:

                cnx = new OleDbConnection();
                cnx.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                    filename + ";Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34;

                string sql = "Select * from [" + tbl + "]";
                OleDbDataAdapter da = new OleDbDataAdapter(sql, getConnection());
                DataSet dst = new DataSet();
                Grid2.DataSource = PivotTable(dst);

I have a screenshot of what this looks like. My intention is to let the user select the values in the drop down to map to the corresponding headers of the uploaded Excel file, so the system can import the Excel file into the database. This is where I'm stuck... Say I have a hashtable that gets populated with the table column name and the Excel headers, how can I use that to write the Excel records to the database with the right Excel columns going to the right db columns?
Question by:bemara57
1 Comment

Accepted Solution

jose_juan earned 2000 total points
ID: 22928410

I suggest to you create a temp Table to cache the Excel data, then your application load data from SQL Server table (not from Excel directly).

To load Excel to your SQL Server table you have many ways ( DTS, bcp, ...) I suggest to you other good way (see code), this is valid if your SQL Server have filesystem access to Excel file.

Good luck!

NOTE: you method (insert data using C#) is not good. Not in transaction!

-- Query to create temp Table
CREATE TABLE myTempExcel (
-- Query to update data from Excel to myTempExcel
	INSERT INTO myTempExcel (
	SELECT	...cols...
			'Excel 8.0; Database=C:\excelData\data.xls',
-- To read data from table into your app
  It's trivial!!

Open in new window


Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question