Problem
How to write code import data table to SQL server 2012 .
Details
I have excel sheet name InvoiceData.xlsx have two columns
UnitCode CurrentMeterReading
21544 2900
22152 9000
19822 9200
I get success data from excel sheet to datatable as following :
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
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 :
System.Data.DataTable dt = new System.Data.DataTable();
dt = Showdataprint();
What I have tried:
How to Import data to sql from Excel data table by c#