Click here to Skip to main content
15,922,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have developed my code with update process using foreach loop. But i need to process this operation using bulk update. Kindly solve this code.

update Datatable contains

C#
ID       Type ID   status    Mismatchvalues    Integration
1          12                           
2          14

Column Datatable contains

C#
Integration      Mismatchvalues      status    ID Type ID
test               15               active     1   12
test1              14               active     2   14

foreach (DataRow items in Update.Rows)
               {

                   foreach (DataRow item in column.Rows)
                   {

                       if ((items.ItemArray[0].ToString().Trim() == item.ItemArray[3].ToString().Trim()) && (items.ItemArray[1].ToString().Trim() == item.ItemArray[4].ToString().Trim()))
                       {
                           Utility.SQLUtility.ExecuteNonQuery(strConnectionOrgconnname, @"update [Test]
                    set  status ='" + item.ItemArray[2].ToString().Trim() + "' , Mismatchvalues ='" + item.ItemArray[1].ToString().Trim() + "',Integration ='" + item.ItemArray[0].ToString().Replace("'", "''").Trim() + "' where [ID] ='" + items.ItemArray[0].ToString().Trim() + "' and [ID]='" + items.ItemArray[1].ToString().Trim() + "'");

                       }
                   }
               }


What I have tried:

foreach (DataRow items in Update.Rows)
                {

                    foreach (DataRow item in column.Rows)
                    {

                        if ((items.ItemArray[0].ToString().Trim() == item.ItemArray[3].ToString().Trim()) && (items.ItemArray[1].ToString().Trim() == item.ItemArray[4].ToString().Trim()))
                        {
                            Utility.SQLUtility.ExecuteNonQuery(strConnectionOrgconnname, @"update [Test]
                     set  status ='" + item.ItemArray[2].ToString().Trim() + "' , _Mismatchvalues ='" + item.ItemArray[1].ToString().Trim() + "',Integration ='" + item.ItemArray[0].ToString().Replace("'", "''").Trim() + "' where [ID] ='" + items.ItemArray[0].ToString().Trim() + "' and [ID]='" + items.ItemArray[1].ToString().Trim() + "'");

                        }
                    }
                }
Posted
Updated 9-Jul-18 8:46am

Your current method is at risk for SQL Injection; you should never build build your SQL Command text with inline variables. The recommended way is to use a Parameterized Query.

One of the features added with SQL 2008 was the ability to use a Table-Value Parameter. This will allow you to pass your entire DataTable as a parameter to a Stored Procedure.

I don't have the time at the moment to go through and do all of this for you; but I can provide the link to MS which has information and samples on how you can do this yourself.

Table-Valued Parameters | Passing a Table-Valued Parameter to a Stored Procedure[^]
 
Share this answer
 
C#
Utility.SQLUtility.ExecuteNonQuery(strConnectionOrgconnname, @"update [Test]
                     set  status ='" + item.ItemArray[2].ToString().Trim() + "' , _Mismatchvalues ='" + item.ItemArray[1].ToString().Trim() + "',Integration ='" + item.ItemArray[0].ToString().Replace("'", "''").Trim() + "' where [ID] ='" + items.ItemArray[0].ToString().Trim() + "' and [ID]='" + items.ItemArray[1].ToString().Trim() + "'");

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Instead of using the foreach loops to pass each separate SQL update command to the ExecuteNonQuery method individually,
use them to build a string that upon completion will contain every update command. The pass that string to ExecuteNonQuery outside the foreachs (if the string isn't null).
 
Share this answer
 
v2
Comments
MadMyche 9-Jul-18 13:12pm    
SQL Injection. The OP's current method is building strings for their commands is a textbook example of what not to do. This I fear will only make it worse.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900