In my sqlserver table i have following columns defined, stationid,dateofevent,itemname,sitename,clicks
To populate above table , we have a c# application. In which inserts data in a loop. Data comes from remote machine and once data received by server(another c# application) , it imserts into sql server and send back OK response to remote client. When client receives response , it archives data into another table and deletes the data from actual table.
Incase if client fails to archive , stored procedure from server side will take care of preventing duplicate record insert.
Set @previousClickCount=( SELECT Clicks FROM [Analytics] as pc
where DATEADD(dd, 0, DATEDIFF(dd, 0, [DateOfEvent]))=@date
and ItemType=@type
and stationId = @stationId
and ItemName=@itemName
and SiteName=@siteName)
If @previousClickCount Is Null
Begin
-- Row for this item is not found in DB so inserting a new row
Insert into Analytics(StationId,DateOfEvent,WeekOfYear,MonthOfYear,Year,ItemType,ItemName,Clicks,SiteName)
VALUES(@stationId,@date,DATEPART(wk,@date),DATEPART(mm,@date),DATEPART(YYYY,@date),@type,@itemName,@clicks,@siteName)
End
Later we decided to move to bulk insert in server side code. So that we can avoid looping.
bulkCopy.DestinationTableName = SqlTableName;
bulkCopy.BatchSize = 1000;
bulkCopy.BulkCopyTimeout = 1000;
bulkCopy.WriteToServer(dataTable);
But incase client side failed to archive data then server will insert duplicate record.Is there any way to check this in bulk insert or whether can we add any constraint like,insert only if the itemname not present for the particular date then insert.
Regards Sangeetha