Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello All

I am working on a c# windows form application. It requires me to import data from excel sheet to sql server through the application. What would be an optimized way to do the same?

However, before I insert the data from excel I need check check whether a duplicate record exists in the table or not. If it already exists, I ignore that row and proceed ahead with insertion of unique rows.

Any pointers would be greatly appreciated.


Thanks
Posted
Updated 3-Mar-14 6:32am
v2
Comments
Maciej Los 3-Mar-14 12:44pm    
What have you done till now? Where are you stuck?
KingOfKop 3-Mar-14 12:50pm    
I am able to retrieve the data from an excel to a data table. However I am not showing it in my application(or a gridview) as it should directly go to DB on button click.

However, before I insert, I need to verify in DB if duplicates exists or not. I am stuck at this point. I also need to prompt the user the list of duplicates (maybe in a message box) after the operation is completed.

I advice you to bulk insert into a temporary table then make it unique (and other checks if you like) on the database. I think this technique both faster and safer.

BULK INSERT[^]

and see here: http://stackoverflow.com/questions/13124680/how-to-bulk-insert-from-xlsx-file-extension[^]
 
Share this answer
 
Comments
Maciej Los 3-Mar-14 13:05pm    
Nice, but OP wants to copy data using WinForm. A4!
Vedat Ozan Oner 3-Mar-14 14:32pm    
yes. winform. I didn't write all steps:
1- interop.excel -> saveas(temp.csv)
2- ado.net bulk insert(temp.cvs)
3- ado.net insert unique raws to real table.
4- check them (with user)
or some other combinations :)
since OP doesn't provide any code, I've just provided a possible solution.
Good to go...
http://support.microsoft.com/kb/302084[^]

http://www.aspsnippets.com/Articles/Read-and-Import-Excel-File-into-DataSet-or-DataTable-using-C-and-VBNet-in-ASPNet.aspx[^]
private void Import_To_Grid(string FilePath, string Extension, string isHDR)
{
    string conStr="";
    switch (Extension)
    {
        case ".xls": //Excel 97-03
            conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"]
                     .ConnectionString;
            break;
        case ".xlsx": //Excel 07
            conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"]
                      .ConnectionString;
            break;
    }
    conStr = String.Format(conStr, FilePath, isHDR);
    OleDbConnection connExcel = new OleDbConnection(conStr);
    OleDbCommand cmdExcel = new OleDbCommand();
    OleDbDataAdapter oda = new OleDbDataAdapter();
    DataTable dt = new DataTable();
    cmdExcel.Connection = connExcel;
 
    //Get the name of First Sheet
    connExcel.Open();
    DataTable dtExcelSchema;
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
    connExcel.Close();
 
    //Read Data from First Sheet
    connExcel.Open();
    cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
    oda.SelectCommand = cmdExcel;
    oda.Fill(dt);
    connExcel.Close();
 
    //Bind Data to GridView
    GridView1.Caption = Path.GetFileName(FilePath);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}


Simple workaround:
-> Simply declare and fill a datatable
-> insert one by one record by checking the duplicates
--> If record not exists, go and add to datatable
--> If record exists, leave and go to Next
-> you can bind the datatable to SQL table
 
Share this answer
 
Comments
Maciej Los 3-Mar-14 13:04pm    
"insert one by one record by checking the duplicates" - inefficient solution! That's the reason of 3!
Here is very interesting article: Import MS Excel data to SQL Server table using C#[^]. Chnage the code to your needs ;)
 
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