如何在C#中使用批量更新 [英] How to use bulk update in C#

查看:140
本文介绍了如何在C#中使用批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用foreach循环开发了更新过程的代码。但我需要使用批量更新来处理此操作。请解决此代码。



更新数据表包含



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

ID       Type ID   status    Mismatchvalues    Integration
1          12                           
2          14



列数据表包含




Column Datatable contains

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() + "'");

                        }
                    }
                }

推荐答案

您当前的方法存在风险SQL注入;你永远不应该用内联变量构建你的SQL命令文本。推荐的方法是使用参数化查询。



SQL 2008添加的功能之一是能够使用表值参数。这将允许您将整个DataTable作为参数传递给存储过程。



我现在没时间通过并为您完成所有这些工作;但我可以提供MS的链接,其中包含有关如何自行完成此操作的信息和样本。



表值参数|将表值参数传递给存储过程 [ ^ ]
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[^]


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() + "'");



不是您问题的解决方案,而是您遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]

我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]


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[^]


而不是使用foreach循环将每个单独的SQL更新命令单独传递给ExecuteNonQuery方法,

使用它们来构建一个字符串,完成后将包含每个更新命令。将字符串传递给foreachs之外的ExecuteNonQuery(如果字符串不为null)。
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).


这篇关于如何在C#中使用批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆