question

Harsha24 avatar image
Harsha24 asked

How to replace existing sql table data with the data uploaded from Excel Sheet through C# code

Hiii I have a table in sqlserver DB namely EmpSalary and it consists of following columns.. EmpID EmpName Jan13 Feb13 Mar13 It is being imported from an excel sheet through .aspx page using C# programming. and now i want to insert another Excel Sheet into the same table .. But now before inserting the new Excel sheet into the table, i want to delete all the old data present in the table and insert the new data. or I just want to check both old data and new data and append any new column(s) in the new data to the old data.. Like if the new data from excel has the following columns EmpID EmpName Jan13 Feb13 Mar13 Apr13 then i want to append Apr13 column(along with data) to the old data in the EmpSalary table. How to do this using C# programming.
sql-serverc#asp.net
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Magnus Ahlkvist avatar image
Magnus Ahlkvist answered
If I was you, I would seriously consider changing the data model. Create one table with employees (EmpID as primary key, name etc as other columns). One table with EmployeeSalaries, with compound key EmpID, Year and Month, and Salary as a numeric column. That way, you won't have to change the data model each month, you can use your data model to look at a rolling average without hardcoding column names etc. SQL Server is a relational database engine. If you indeed have good reasons for your data model (I don't think so), you can issue **ALTER TABLE ADD columnname datatype** from your C#-code.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.