Posts

Showing posts from September, 2016

AP.NET WEBAPI: Compress a CSV file and Send the Response as ZIP

WEB API: Compress a CSV file and Send the Response as ZIP In ASP.NET WEB API, we can create an API call for sending the file in  response. Which means that the user can call an URL and gets the required file as a download. Zipping a file in earlier versions of dotnet framework 4.0 was quite lengthy (sometimes it has needed to use third part tools). From 4.5 on wards, we have an assembly for that task specifically with one single line of code. C# code for the API: We need to add reference of System.IO.Compression.FileSystem to the project and use the below using statement to make the ZIP statement work. using System.IO.Compression;   Once the steps are done, need to map the directory to store the generated CSV file temporarily. Once mapping is done, write all text content the the file. Need to provide the final zip file name along its path (fully qualified file name). Zip.CreateFromDirectory is the important line to ZIP. It just takes the source folder contents, zips it and

Efficient / Alternate Inserts with NOT EXISTS or EXISTS condition in SQL Server (UPSERT with MERGE Statement)

Efficient / Alternate Inserts with NOT EXISTS or EXISTS condition in SQL Server (UPSERT with MERGE Statement) Normally, we are inserting records into a target table, to avoid duplicates, we use a NOT EXISTS or EXISTS condition. This condition work good if the target table has less number of records.  IF the Target table has millions of records, this exists condition becomes a costly operation as it needs to check with every record in table while inserting the current record. To avoid this performance issue, we make use of MERGE statement in SQL Server.  In the below query, we can insert when the records into target table when that record is not available and can update the record when it is already available.  Source Table: col1,col2,joiningCol1,joiningCol2 Target Table: col3,col4,JoiningCol1,joiningCol2 MERGE INTO TargetTable AS Target  USING(   SELECT  *   FROM SourceTable    WHERE  col1>1)   AS Source   ON Source.joiningCol1 = Target.joiningCol1 AND joiningCol2=Target.jo