C# 和 ADO.NET 中 System.Data.DataRow 的缓慢更新

发布于 2024-09-30 14:01:30 字数 625 浏览 1 评论 0原文

我在一个简单的 C# 应用程序中使用 ADO.NET。

有两个表,TableA 和TableB。 TableA 是 Table B 的父表。TableA 包含:

  1. id 作为主键 (Int32)
  2. 其他列。我认为这无关紧要,所以我不会详细说明。

表 B 具有以下列:

  1. id (主键) (Int32)
  2. tableAid (与表 A 的外键关系)(Int32 和主键)
  3. X (双精度型)
  4. Y (双精度型)

我在表 B 中创建了大约 300 行。想要更新 X 和 Y 的列值,使每行具有相同的值。我目前正在这样做:

TableBRow[] rowsOfB = TableA.GetTableBRows();

for (int i = 0 ; i < rowsOfB.Length ; i++) 
{
    rowsOfB[i].X = newXvalue; 
    rowsOfB[i].Y = newYvalue; 
}

这段代码似乎需要很长时间才能运行。我的问题是(i)为什么这么慢? (ii) 更新表中多行的推荐方法是什么?有没有批量更新的方法。

I'm using ADO.NET in a simple C# application.

There are two tables, TableA and TableB. TableA is a parent of Table B. TableA contains:

  1. id as a primary key (Int32)
  2. Other columns. I think it's irrelevant so I won't elaborate.

Table B has these columns:

  1. id (primary key) (Int32)
  2. tableAid (foreign key relationship with table A) (Int32 and primary key)
  3. X (double type)
  4. Y (double type)

I have created approximately 300 rows in table B. I want to update the columns values for X and Y to have the same value for each row. I'm currently doing this:

TableBRow[] rowsOfB = TableA.GetTableBRows();

for (int i = 0 ; i < rowsOfB.Length ; i++) 
{
    rowsOfB[i].X = newXvalue; 
    rowsOfB[i].Y = newYvalue; 
}

This code seems to take a long time to run. My questions are (i) why is this slow ? and (ii) what is the recommended way of updating many rows in a table? Is there a bulk update approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2024-10-07 14:01:30

如果您正在进行大型更新,通常会创建一个临时表,进行批量插入,加入此临时表并执行更新比执行 n 次更新更快。

if you're doing a large update, generally creating a temp table, doing a bulk insert, join on this temp table and do your update is quicker than doing n updates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文