Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In my application, I have more than 160 fields, I want to enable user to select few of fields and then he can import data from excel to database.
How can I achieve the same as I don't want to create a procedure with 160 parameters and then managing transformation within stored proc.

Is there any dynamic way of doing this? Through which I can manage things without giving 160 parameters.

What I have tried:

The solution which I can think of is to create a procedure with 160 parameters and have 1 table which will map the parameters and excel column and then insert data into database.
Posted
Updated 1-Aug-16 16:00pm
Comments
PeejayAdams 1-Aug-16 10:52am    
I would ask yourself the following questions:

1) Is Excel really the tool for the job? If yes, then you'll need to look into SSIS as a way to export from Excel to SQLServer. It won't be dynamic but you can automate the export of saved spreadsheets. It's not entirely clear what your requirement is but if you're trying to allow someone to maintain data, it's far, far better to do that via a form based app where you have control over validation etc.

2) Do you really want users to decide what fields get exported? (Hint: the answer you're looking for is a very firm "no!")

3) Do you need to update all 160 columns in a single procedure? I very much hope that your database doesn't have a 160+ column table. If it does, rewrite it/get it rewritten before you go any further.
ZurdoDev 1-Aug-16 12:12pm    
Sql can parse json and xml so you could always go that route.

1 solution

Office Import Excel Spreadsheet data into Sql Server table via C# and vb.net sample in C#, VB.NET, SQL for Visual Studio 2013[^] sample code and project

using System.Data.OleDb;
using System.Data.SqlClient;


public void ImportDataFromExcel(string excelFilePath)
{
//declare variables - edit these based on your particular situation
string ssqltable = "Table1";
// make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have different
string myexceldataquery = "select student,rollno,course from [Sheet1$]";
try
{
//create our connection strings
string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelFilePath +
";extended properties=" + "\"excel 8.0;hdr=yes;\"";
string ssqlconnectionstring = "Data Source=SAYYED;Initial Catalog=SyncDB;Integrated Security=True";
//execute a query to erase any previous data from our destination table
string sclearsql = "delete from " + ssqltable;
SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();
//series of commands to bulk copy data from the excel file into our sql table
OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
bulkcopy.DestinationTableName = ssqltable;
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
dr.Close();
oledbconn.Close();
Label1.Text = "File imported into sql server.";
}
catch (Exception ex)
{
//handle exception
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900