to achieved the desire functionality follow below steps
create a type in sql like this should be same as your excel file format :
create type type_ItemTable as table
(
ItemCode nvarchar(50),
ItemName nvarchar(100)
)
then create a procedure in sql as below
create Procedure sp_InsertBulkItemNotExist
(
@Details type_ItemTable ReadOnly
)
as
begin
Select * into #Temp from ItemTable
Insert into ItemTable(ItemCode,ItemName)
Select ItemCode,ItemName from @Details where ItemCode Not in
(Select ItemCode from ItemTable)
Select * from @Details where ItemCode in (Select ItemCode from #Temp)
end
then in you showexceldata()
public DataTable ShowExcelData()
{
string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
DataTable dt = new DataTable();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand com = new OleDbCommand();
com.Connection = con;
com.CommandText = @"SELECT [ItemCode],[ItemsName] FROM [" + SheetName + "] ";
OleDbDataAdapter oledbda = new OleDbDataAdapter();
oledbda.SelectCommand = com;
DataTable dtReturn = new DataTable();
oledbda.Fill(dtReturn);
con.Close();
SqlConnection con = new SqlConnection("YourConnectionstring");
SqlCommand cmd = new SqlCommand("sp_InsertBulkItemNotExist", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Details", dtReturn);
cmd.CommandTimeout = 0;
SqlDataAdapter adap = new SqlDataAdapter(cmd, con);
dt=new DataTable();
adap.Fill(dt);
return dt;
}
then at time of Importing Excel Sheet
just call ShowExcelData() as
DataTable dt=ShowExcelData();
after execution of above state all new items will be inserted into ItemTable and already exist Item will return in dt