In this article, I am going to discuss How to Perform BULK INSERT and UPDATE in C# and ADO.NET using SQL Server Stored Procedure with Examples. Please read our previous article where we discussed ADO.NET Architecture. Here, in this article, I will explain to you how to perform the Bulk Insert and Update using the Stored Procedure with Examples, and in the next article, I am going to discuss how to Perform Batch Operations Using SqlBulkCopy and DataAdapters.
Let us understand how to perform Bulk Insert and Update using C# and ADO.NET. So, basically what we will do is, we will check if the record does not exist in the database, then we will insert that record and if the record exists in the database, then we will update that record and we are going to perform this as bulk. That means we are having a data table and that data table contains many records (new records as well as existing records). The new records need to be inserted into the database while the existing records need to be updated in the database.
Here, we will create a stored procedure that will accept Table Type Parameter. The SQL Server Table Valued Parameter allows us to pass multiple records using a DataTable to the Stored Procedure and then within the stored procedure using the Table Type Parameter we are going to perform the Bulk Insert and Update Operations. Let us understand this step by step.
We are going to use the following Employee table.
Please use the below SQL Script to Create the EmployeeDB database and Employee table and Populate the Employee table with the required sample data.
As we are going to perform the BULK Insert and Update operation using a stored procedure, so we need a Table Type Parameter.
A Table-Type Parameter is a database object in SQL Server and it provides an easy way to pass multiple rows of data from a client application to an SQL Server Database without multiple round trips. That means we can use a Table-Type Parameter to encapsulate rows of data in a client application and send we can send those rows of data to the SQL Server Database in a single request. Once the SQL Server received the data, then the SQL Server process the data.
Let us create a Table Type Parameter within the SQL Server Employee database to handle multiple rows of Employee data. Please use the below SQL Script to create the EmployeeType Table Type Parameter.
Once you created the User Defined Table Type Parameter, then it will be visible in the Object Explorer as shown in the below image.
Now, we need to create a stored procedure that will accept our user-defined table type as a parameter and using that user-defined table type parameter we are going to perform the Bulk Insert and Update operations. If the record exists, it is going to update the record based on the Id and if the record not exists then it is going to insert the record into the database.
Again, we can implement the logic in two ways. If you are using SQL Server 2008 or a Higher version, then you can use the MERGE statement to implement the logic and if you are using SQL Server 2005 or any lower version, then you cannot use the MERGE statement. MERGE statement is going to give you better performance. Let us see how we can implement the logic using MERGE and without using the MERGE function.
SQL Server 2008 came up with a new function called MERGE, which allows performing INSERT operation when records are not present and UPDATE operation when records are present in the table. In the below Stored Procedure, we are using the MERGE function to perform the BULK INSERT and UPDATE operations.
Note: When you use SET NOCOUNT ON, the message that indicates the number of rows that are affected by the T-SQL statement is not returned as part of the results. When you use SET NOCOUNT OFF; the count is returned. Using SET NOCOUNT ON can improve performance because network traffic can be reduced.
If you are stuck with SQL Server 2005 or any lower version, then you cannot use the MERGE function. In the below-stored procedure, it first performs the UPDATE operations using INNER JOIN and then performs the INSERT operation for those records which are not present inside the table.
So, our database part is over. We have created our database, and tables, and populated the table with some sample data. We have also created the user-defined Table Type and user-defined stored procedure where we have implemented the logic to perform the BULK INSERT and UPDATE Operations. Now, let us proceed and try to understand how we can call that stored procedure from our C# application to perform the Bulk INSERT and UPDATE operation.
Let us understand how to call the stored procedure using the data table as a parameter. As the stored procedure excepts one Table Type Parameter, so from our C# code, we can create a data table and we can that data table as an input parameter to the stored procedure. The following example code is self-explained, so please go through the comment lines.
Once you execute the above code, then check the Employee table in the database and now the Employee should have the updated as well as employee data as shown in the below image.
Note: In the above example, we have hard-coded the data table data, in our upcoming article, I will show you how to fill the data table data from an Excel file.
In the next article, I am going to discuss How to Perform Bulk INSERT using SqlBulkCopy Object in C# ADO.NET with Examples. Here, in this article, I try to explain how to perform Bulk INSERT and UPDATE using SQL Server Stored Procedure and DataTables in C#. I hope you enjoy this Bulk Insert Update in C# and ADO.NET using Stored Procedure with Examples article.