public void ImportDataFromExcel(string excelFilePath)
{
//declare variables - edit these based on your particular situation
string ssqltable = "SP_GES_Usersrequest";
DataTable dtSQlbulk = new DataTable();
// 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 * from [Sheet1$]";
string myexceldataquery = string.Format("Select * FROM [{0}]", "Sheet1$");
// string myexceldataquery = "SELECT Request#,User,Application,Data's BU,Instance or Module,Group,Role to Add.:Role,Role to Remove.:Role,User Manager,Manager Approval Status,Manager Statement Date,Group Owner Name,Owner Approval Status,Owner Statement Date,IT Owner Name,IT Owner Approval Status,IT Owner Statement Date,Role to Add.:RoleUniqueFilterinApp,Role to Remove.:RoleUniqueFilterinApp,Role to Add.:Associated Groups\\Roles to Add,Role to Add.:Associated Groups\\Roles to Remove,Role to Remove.:Associated Groups\\Roles to Add,Role to Remove.:Associated Groups\\Roles to Remove,Item Type,Path 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=***** ;Initial Catalog=*********;User ID=sa;Password=Sep@2019";
//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();
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);
}
}