简体   繁体   English

使用C#如何从SQL数据库中有效检索和更新数千条记录

[英]Using c# how to retrieve and update thousands of records from a SQL database efficiently

Process 处理

  1. I am writing a C# application which will need to retrieve 4 million records(ID) from a SQL table in database A. 我正在编写一个C#应用程序,它将需要从数据库A中的SQL表中检索400万条记录(ID)。
  2. I then need to use each ID to select a row of record each from another SQL table in database B. 然后,我需要使用每个ID从数据库B中的另一个SQL表中选择一个记录行。
  3. Once I have this row I then need to update another SQL table in database C 一旦有了这一行,我就需要更新数据库C中的另一个SQL表

Questions 问题

  1. What's the most efficient way to retrieve and store the data in Step 1? 在步骤1中检索和存储数据的最有效方法是什么? a. 一种。 Should I load this in a list string? 我应该将其加载到列表字符串中吗? b. b。 Do you recommend doing batches initially? 您是否建议先进行批处理?

  2. What the most efficient way to achieve steps 2 and 3 什么是实现步骤2和3的最有效方法

To retrieve the 4M records you're going to want to use a SqlDataReader - it only loads one row of data into memory at a time. 要检索4M记录,您将要使用SqlDataReader一次仅将一行数据加载到内存中。

var cn = new SqlConnection("some connection string");
var cmd = new SqlCommand("SELECT ID FROM SomeTable", cn);
var reader = cmd.ExecuteReader();

while (reader.Read())
{
    var id = reader.GetInt32(0);

    // an so on
}

reader.Close();
reader.Dispose();
cn.Close();

Now, to handle two and three, I would leverage a DataTable for the row you need to retrieve and then a SqlCommand on the third database. 现在,要处理两个和三个,我将在需要检索的行上使用一个DataTable ,然后在第三个数据库上使用一个SqlCommand This means that inside the reader.Read() you can get the one row you need by filling a DataTable with a SqlDataAdapter and the issuing an ExecNonQuery against a SqlCommand for the UPDATE statement. 这意味着在reader.Read()内部,您可以通过用SqlDataAdapter填充DataTable并针对UPDATE语句针对SqlCommand发出ExecNonQuery来获得所需的一行。

Another way of writing the above, and it's a bit safer, is to use the using statement: 编写上述内容的另一种方法(更安全一些)是使用using语句:

using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        var id = reader.GetInt32(0);

        // an so on
    }
}

that will eliminate the need for: 这将消除以下需求:

reader.Close();
reader.Dispose();

and so you could also issue that for the SqlConnection if you wanted. 因此,如果需要,您也可以为SqlConnection发出该消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写 SQL 以使用 C# 从 Access 数据库中检索单个日期的记录 - How do I write the SQL to retrieve records for a single date from Access database using C# 如何在比较数据库中的每一行时有效地使用 C# 在 SQL 服务器中插入/更新 10000 行 - How to insert/Update 10000 rows in SQL Server using C# efficiently while comparing each row from database 如何在C#中更新MDB文件中的数千条记录 - How to UPDATE thousands of records in an MDB file in C# 从.net c#应用程序循环正确有效地更新sql server db表中的数百条记录 - Properly and efficiently update hundreds of records in sql server db table from .net c# application loop 如何有效地从存储过程中检索数千行 - how to retrieve thousands of rows from a stored procedure efficiently 如何在C#中与SQL数据库表之间来回存储和检索数组 - How to store and retrieve an array to and from SQL database table in c# 如何在 C# 中从 SQL Server 数据库中检索数据? - How to retrieve data from a SQL Server database in C#? 如何从sql数据库中的c#中显示记录到2 listview? - how to display records to 2 listview in c# from sql database? 如何使用 C# 从 SQL Server 数据库中逐行检索数据到 datagridview - How to retrieve data into a datagridview row by row from a SQL Server database using C# 如何使用 ASP.NET 从 SQL 服务器数据库中检索和显示 GridView 中的值 - How to retrieve and display values in GridView from SQL Server database in ASP.NET using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM