How to use SqlBulkCopy in C#

Rajkumardotnet90
Posted by Rajkumardotnet90 under C# category on | Points: 40 | Views : 1093
Fname="your Excel file Path"
String sqltable="Destinationsqltable";
If(fname.EndsWith('.xlsx'))
{
Xlcon="Provider=Microsoft.Ace.OLEDB.12.0;Data Source="+fname+";Extended Properties='Excel 12.0";HDR=Yes'";
}
Olcon=New OleDbConnection(xlcon);
OleDbCommand olcmd=New OleDbCommand("select * from [Sheet1$]",Olcon);
OleDbDataReader odr=Olcmd.ExecuteReader();
String sqlcon=your connection str
}

HDR="YES" then it reads from Second ROW..

IF IT DOESN'T READ FIRSTROW MEANS KINDLY CHANGE INTO HDR=NOing coding.
SqlBulkCopy bulkcopy=New SqlbulkCopy(sqlcon);
Bulkcopy.DestinationTableName=sqltable;
While(odr.Read())
{
Bulkcopy.WriteToServer(odr);
}
Olcon.Close();

Comments or Responses

Login to post response