Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem

How to write code import data table to SQL server 2012 .

Details

I have excel sheet name InvoiceData.xlsx have two columns
SQL
UnitCode    CurrentMeterReading
21544                2900
22152                9000
19822                9200

I get success data from excel sheet to datatable as following :

C#
public System.Data. DataTable Showdataprint()
        {

            OleDbConnection con = new OleDbConnection(connectionString);
            con.Open();   
            string str = @"SELECT  [UnitCode],[CurrentMeterReading] FROM  [Sheet1$]";
            OleDbCommand com = new OleDbCommand();
            com = new OleDbCommand(str, con);
            OleDbDataAdapter oledbda = new OleDbDataAdapter();
            oledbda = new OleDbDataAdapter(com);
            DataSet ds = new DataSet();
            ds = new DataSet();
            oledbda.Fill(ds,"[Sheet1$]");
            con.Close();
            System.Data.DataTable dt = new System.Data.DataTable();
            dt = ds.Tables["[Sheet1$]"];
            return dt;

        }

I get successfully data from excel to Datatable Now I need to Import data from datatable to sql server 2012 TableName : WahInvoice PK : Serial

SQL
CREATE TABLE [dbo].[WAHInvoice](
    [Serial] [int] NOT NULL,
    [Year] [int] NULL,
    [Month] [int] NULL,
    [UnitCode] [int] NULL,
    [CurrentReadingDate] [date] NULL,
    [CurrentMeterReading] [decimal](18, 2) NULL,
 CONSTRAINT [PK_WAHInvoice_1] PRIMARY KEY CLUSTERED 
(
    [Serial] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

I need to know how to Import data from excel to sql .

suppose i have data on sql in table WahInvoice

with max serial 2000 this meaning data will be as following :

Serial UnitCode CurrentMeterReading
2001 21544 2900
2002 22152 9000
2003 19822 9200
and all values for columns remaining will be null .

Actually what i write for Import data to sql under import button Under Import button click i write as following :

C#
System.Data.DataTable dt = new System.Data.DataTable();
                    dt = Showdataprint();
//How to import datatable to sql server 2012


What I have tried:

How to Import data to sql from Excel data table by c#
Posted
Updated 7-Mar-18 17:27pm

1 solution

Since you already have the data in a datatable, one easy way is to use SqlBulkCopy class.

Have a look at the SqlBulkCopy.WriteToServer Method (DataTable) (System.Data.SqlClient)[^]. There's a nice example how to write data to the server with this method.

There are also quite a lot of articles about this, for example Bulk Insert into SQL from C#[^]
 
Share this answer
 
Comments
Maciej Los 8-Mar-18 2:04am    
5!
Wendelius 22-Mar-18 0:44am    
Thank you :)
Srilekha Bolamoni 20-Nov-18 4:16am    
I have the data in datatable but I don't want to use sqlbulkcopy because I want only 6 columns and 20 rows . so can you suggest any other way.

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